gpt4 book ai didi

php - Symfony:有什么方法可以使用模板的相对路径来渲染 Twig 模板吗?

转载 作者:可可西里 更新时间:2023-10-31 22:47:54 24 4
gpt4 key购买 nike

Template Naming and Locations Symfony 文档中的部分说:

Symfony2 uses a bundle:controller:template string syntax for templates. This allows for several different types of templates, each which lives in a specific location:

  • AcmeBlogBundle:Blog:index.html.twig: This syntax is used to specify a template for a specific page. The three parts of the string, each separated by a colon (:), mean the following:
  • AcmeBlogBundle: (bundle) the template lives inside the AcmeBlogBundle (e.g. src/Acme/BlogBundle); Blog: (controller) indicates that the template lives inside the Blog subdirectory of Resources/views;
  • index.html.twig: (template) the actual name of the file is index.html.twig.

我想在我的数据夹具引导过程中解析一个 Twig 模板并将 html 保存到学说实体的属性中,如下所示:

// let's say it finds ./Data/Product/camera_description.html.twig
$productDescriptionTemplate = __DIR__.sprintf(
'/Data/Product/%s_description.html.twig',
$product->getName()
);

$product->setDescription(
$this->container->get('templating')->render(
$productDescriptionTemplate,
array()
)
);

$em->flush();

抛出以下异常:

# would actually be an absolute path
[InvalidArgumentException]
Template name "./Data/Product/camera_description.html.twig
" is not valid (format is "bundle:section:template.format.engine").

是的,我可以将产品描述模板移动到 path/to/bundle/Resources/views/ 但我更感兴趣的是是否可以规避此约定:是否有办法提供twig 模板引擎 relative 或 absolute 到 twig 模板的路径并让它呈现它 没有 使用约定 bundle:controller:template

最佳答案

您还可以创建一个新的命名空间,如下所述:http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html

例如:

paths:
"%kernel.root_dir%/../Data/Product/": product

这应该允许你写:

'product::%s_description.html.twig', 

'@product/%s_description.html.twig'

关于php - Symfony:有什么方法可以使用模板的相对路径来渲染 Twig 模板吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22734560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com