未在Symfony2.1中添加基本模板路径

时间:2012-11-05 07:52:04

标签: symfony twig symfony-2.1

未找到模板form_div_layout.html.twig

树枝装载机中的路径:

Array ( [0] => /var/www/nrp.new-generation.com.ua/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views )

为什么不添加默认路径?

错误:

Unable to find template "form_div_layout.html.twig" in Service:AbstractService::layout.html.twig at line 27.

Twig_Loader_Exception:

Twig_Error_Loader: Unable to find template "form_div_layout.html.twig" (looked into: /var/www/{path}/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views).

编辑:

我在TwigLoader中替换默认的TemplateNameParser以添加logik加载模板。

我的TemplateNameParser:

    <?php

namespace NRP\Bundles\ServiceBundle\Twig;

use Symfony\Bundle\FrameworkBundle\Templating\TemplateNameParser as BaseTemplateNameParser,
    Symfony\Component\Templating\TemplateReferenceInterface;

/**
 *
 */
class TemplateNameParser extends BaseTemplateNameParser
{
  /**
   * {@inheritdoc}
   */
  public function parse($name)
  {
    if ($name instanceof TemplateReferenceInterface) {
      return $name;
    }
    else if (isset($this->cache[$name])) {
      return $this->cache[$name];
    }

    // If not service prefix
    if (strpos($name, 'Service:') !== 0) {
      return parent::parse($name);
    }

    // Find template
    $name = str_replace(':/', ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));

    if (false !== strpos($name, '..')) {
      throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
    }

    $parts = explode(':', $name);
    if (4 !== count($parts)) {
      throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "Service:{ServiceName}:{Section}:{template}.{format}.{engine}").', $name));
    }

    $elements = explode('.', $parts[3]);
    if (3 > count($elements)) {
      throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "Service:{ServiceName}:{Section}:{template}.{format}.{engine}").', $name));
    }

    $engine = array_pop($elements);
    $format = array_pop($elements);

    $template = new TemplateReference($parts[1], $parts[2], implode('.', $elements), $format, $engine);

    if (!$template->hasService()) {
      throw new \RuntimeException(sprintf('Not found service "%s".', $template->get('service')));
    }

    return $this->cache[$name] = $template;
  }
}

并添加到服务:

<!-- Twig file system loader -->
<service id="nrp.twig.name_parser" class="NRP\Bundles\ServiceBundle\Twig\TemplateNameParser">
  <argument type="service" id="kernel" />
</service>
<service id="twig.loader" class="Symfony\Bundle\TwigBundle\Loader\FilesystemLoader">
  <argument type="service" id="templating.locator" />
  <argument type="service" id="nrp.twig.name_parser" />
</service>

如果禁用此加载程序,一切正常!

1 个答案:

答案 0 :(得分:2)

这个问题解决了!

解决方案:

替换参数templating.name_parser.class;

示例:

<parameters>
    <!-- Parameter for TemplatingNameParser -->
    <parameter key="templating.name_parser.class">NRP\Bundles\ServiceBundle\Twig\TemplateNameParser</parameter>
  </parameters>