螺栓中的内容类型

Content Type's in bolt

我试图在 bolt 中为 2 种内容类型提供相同的单一 slug。它们都应该有相同的字段和相同的模板。此时我使用 record_template: 标签为每个内容类型设置默认模板。

vervolgpagina:
  name: vervolgpagina
  singular_slug: graszoden
  singular_name: vervolg
  fields:
      titel:
          type: text
          class: large
          group: Content
      slug:
          type: slug
          uses: titel
          group: Content
      body:
          type: html
          height: 500px
          group: Content
      meta_keywords:
          type: text
          group: SEO
          label: "Meta keywords"
      meta_description:
          type: textarea
          height: 50px
          group: SEO
          label: "Meta description"
  record_template: vervolgpagina.twig

graszoden:
  name: graszoden
  singular_slug: graszoden
  singular_name: gras
  fields:
      titel:
          type: text
          class: large
          group: Content
      slug:
          type: slug
          uses: titel
          group: Content
      body:
          type: html
          height: 500px
          group: Content
      meta_keywords:
          type: text
          group: SEO
          label: "Meta keywords"
      meta_description:
          type: textarea
          height: 50px
          group: SEO
          label: "Meta description"
  record_template: vervolgpagina.twig

所以如果我只输入两种内容类型中的一种并向其添加一个页面,那么它 将我重定向到 /graszoden/vervolgpagina

所以如果我添加第二个内容类型并向其添加另一个页面/graszoden/randomexample

现在发生了什么。是工作正常的页面。说 ERROR 404,另一页给我一个螺栓错误 "No listing.twig file specified."

有没有办法在 cms 中用其他名称显示两次内容类型?或者有没有办法让两种内容类型具有相同的单数 slug?

提前致谢,

不,你不能。如果您有两个具有相同 slug 和相同字段的内容类型,那么它实际上应该只是一个 CT。

也许您想要的是 "Grouping Taxonomy",请参见 taxonomy.yml 中的示例。

在使用 twig 在 bolt 中进行更多项目后,我得到了一些其他解决方案。

如果您想多次使用单个 slug,您将不得不创建一个列表页面。但是有了这个你就不必了。只需创建一个本地扩展并填写此答案末尾突出显示的部分。

Link to sourcecode 还没有自述文件。但我希望这里的解释足够好。

这是扩展文件代码(这段代码几乎是所有功能,您必须创建一个本地扩展才能工作):

class MatchAllController
{
/**
 * The bolt application
 *
 * @var
 */
protected $app;

/**
 * The path to the config.yml
 *
 * @var string
 */
protected $configPath = __DIR__ . '/../../config/config.yml';

public function matchAll(Request $request, $slug, Application $application)
{
    $this->app = $application;

//        var_dump($this->getConfig());die;
    $contentTypes = ['provincies', 'vervolg'];

    foreach($contentTypes as $contentType)
    {
        if(null !== $result = $this->record($request, $contentType, $slug)) {
            return $result;
        }
    }

    $this->abort(Response::HTTP_NOT_FOUND, "Page $slug not found.");
    return null;
}

protected function getConfig()
{
    return Yaml::parse(file_get_contents($this->configPath));
}

public function record(Request $request, $contenttypeslug, $slug = '')
{
    $contenttype = $this->getContentType($contenttypeslug);

    // Perhaps we don't have a slug. Let's see if we can pick up the 'id', instead.
    if (empty($slug)) {
        $slug = $request->get('id');
    }

    $slug = $this->app['slugify']->slugify($slug);

    // First, try to get it by slug.
    $content = $this->getContent($contenttype['slug'], ['slug' => $slug, 'returnsingle' => true, 'log_not_found' => !is_numeric($slug)]);

    if (!$content && is_numeric($slug)) {
        // And otherwise try getting it by ID
        $content = $this->getContent($contenttype['slug'], ['id' => $slug, 'returnsingle' => true]);
    }

    // No content, no page!
    if (!$content) {
        return null;
    }

    // Then, select which template to use, based on our 'cascading templates rules'
    $template = $this->templateChooser()->record($content);

    // Setting the canonical URL.
    if ($content->isHome() && ($template === $this->getOption('general/homepage_template'))) {
        $url = $this->app['resources']->getUrl('rooturl');
    } else {
        $url = $this->app['resources']->getUrl('rooturl') . ltrim($content->link(), '/');
    }
    $this->app['resources']->setUrl('canonicalurl', $url);

    // Setting the editlink
    $this->app['editlink'] = $this->generateUrl('editcontent', ['contenttypeslug' => $contenttype['slug'], 'id' => $content->id]);
    $this->app['edittitle'] = $content->getTitle();

    // Make sure we can also access it as {{ page.title }} for pages, etc. We set these in the global scope,
    // So that they're also available in menu's and templates rendered by extensions.
    $globals = [
        'record'                      => $content,
        $contenttype['singular_slug'] => $content,
    ];

    return $this->render($template, [], $globals);
}

/**
 * Get the contenttype as an array, based on the given slug.
 *
 * @param string $slug
 *
 * @return boolean|array
 */
protected function getContentType($slug)
{
    return $this->storage()->getContentType($slug);
}

/**
 * Returns the Entity Manager.
 *
 * @return \Bolt\Storage\EntityManager
 */
protected function storage()
{
    return $this->app['storage'];
}

/**
 * Shortcut to abort the current request by sending a proper HTTP error.
 *
 * @param integer $statusCode The HTTP status code
 * @param string  $message    The status message
 * @param array   $headers    An array of HTTP headers
 *
 * @throws HttpExceptionInterface
 */
protected function abort($statusCode, $message = '', array $headers = [])
{
    $this->app->abort($statusCode, $message, $headers);
}

/**
 * Shortcut for {@see \Bolt\Legacy\Storage::getContent()}
 *
 * @param string $textquery
 * @param array  $parameters
 * @param array  $pager
 * @param array  $whereparameters
 *
 * @return \Bolt\Legacy\Content|\Bolt\Legacy\Content[]
 *
 * @see \Bolt\Legacy\Storage::getContent()
 */
protected function getContent($textquery, $parameters = [], &$pager = [], $whereparameters = [])
{
    return $this->storage()->getContent($textquery, $parameters, $pager, $whereparameters);
}

/**
 * Return the Bolt\TemplateChooser provider.
 *
 * @return \Bolt\TemplateChooser
 */
protected function templateChooser()
{
    return $this->app['templatechooser'];
}

/**
 * Shortcut for {@see UrlGeneratorInterface::generate}
 *
 * @param string $name          The name of the route
 * @param array  $params        An array of parameters
 * @param int    $referenceType The type of reference to be generated (one of the constants)
 *
 * @return string
 */
protected function generateUrl($name, $params = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
    /** @var UrlGeneratorInterface $generator */
    $generator = $this->app['url_generator'];

    return $generator->generate($name, $params, $referenceType);
}

/**
 * Renders a template
 *
 * @param string $template  the template name
 * @param array  $variables array of context variables
 * @param array  $globals   array of global variables
 *
 * @return \Bolt\Response\BoltResponse
 */
protected function render($template, array $variables = [], array $globals = [])
{
    return $this->app['render']->render($template, $variables, $globals);
}
}

因为这是一个新的开始项目。唯一可变的是这部分

public function matchAll(Request $request, $slug, Application $application)
{
$this->app = $application;

//Fill your costum routes in here
$contentTypes = ['VARIABLE SINGULAR SLUG', 'VARIABLE SINGULAR SLUG 2'];

foreach($contentTypes as $contentType)
{
    if(null !== $result = $this->record($request, $contentType, $slug)) {
        return $result;
    }
}

$this->abort(Response::HTTP_NOT_FOUND, "Page $slug not found.");
return null;
}

$contentTypes = ['VARIABLE SINGULAR SLUG', 'VARIABLE SINGULAR SLUG 2'];

这段特定的代码是您填写要使用的单数别名的地方。它可以包含无限 slug

希望这对以后的其他人有所帮助。编码愉快!