包含一个树枝文件并从一个单独的文件传递变量? [英] Include a twig file and pass variables from a separate file?
问题描述
我有container.twig包含component.twig并传递一个名为'mock'的对象。
在container.twig中:
{%set mock = {
title:这是我的标题
}
%}
{%include'component.twig'with mock%}
这工作正常,但我想将模拟数据移动到它自己的文件。这不是工作:
Container.twig
{%include' component.twig'with'mock.twig'%}
在mock.twig中
{%set mock = {
title:这是我的标题
}
%}
我使用gulp-twig,但它在大多数方面都像标准树枝一样工作。 https://github.com/zimmen/gulp-twig
小枝上下文永远不会存储在模板对象中,所以这将很难找到干净的方式为了达成这个。例如,以下Twig代码:
{%set test ='Hello,world'%}
code>
将编译为:
<?php
$ b $ class __TwigTemplate_20df0122e7c88760565e671dea7b7d68c33516f833acc39288f926e234b08380 extends Twig_Template
{
/ * ... * /
保护函数doDisplay(array $ context,array $ blocks = array())
{
// line 1
$ context [test] =Hello,world;
}
/ * ... * /
}
正如您所看到的,继承的上下文不会通过引用传递给doDisplay方法,也不会存储在对象本身中(例如 $ this-> context = $ context
)。此设计允许模板可重复使用,并且内存友好。
解决方案1:使用全局变量
我不知道您是否知道全局变量。你可以用它们做一堆黑客。
最简单的用法是在你的树枝环境中加载所有的全局变量。
$ loader = new Twig_Loader_Filesystem(__ DIR __。'/ view');
$ env = new Twig_Environment($ loader);
$ env-> addGlobal('foo','bar');
$ env-> addGlobal('Hello','world!');
然后,您可以使用 {{foo}}
和 {{Hello}}
。
-
当您试图从twig文件加载变量时,我认为您有很多变量需要初始化,具体取决于您的功能,我不想随时加载所有内容。
-
您正在从PHP脚本中加载变量,而不是从Twig中加载变量,并且您的问题想要从一个twig文件中导入变量。
解决方案2:使用Twig扩展
您还可以创建一个存储扩展,一个 save
函数将某个模板的上下文保存在某个地方,另一个 restore
函数将这个存储的上下文合并到另一个上。 / b>
proof_of_concept.php
< ?php
require __DIR __。'/ vendor / autoload.php';
类StorageTwigExtension扩展了Twig_Extension
{
protected $ storage = [];
public function getFunctions(){
return [
new Twig_SimpleFunction('save',[$ this,'save'],['needs_context'=> true]) ,
new Twig_SimpleFunction('restore',[$ this,'restore'],['needs_context'=> true]),
];
public function save($ context,$ name){
$ this-> storage = array_merge($ this-> storage,$ context);
public function restore(& $ context,$ name){
$ context = array_merge($ context,$ this-> storage);
}
public function getName(){
return'storage';
$ b $ *使用示例* /
$ loader = new Twig_Loader_Filesystem(__ DIR __。'/ view');
$ env = new Twig_Environment($ loader);
$ env-> addExtension(new StorageTwigExtension());
echo $ env-> render('test.twig'),PHP_EOL;
twig / variables.twig
{%set foo ='bar'%}
{%set Hello ='world!'%}
{{save('test ')}}
twig / test.twig
{%include'variables.twig'%}
{{restore('test')}}
{{foo注意:如果您只想导入变量而不真正呈现内部内容 twig / variables.twig
,您也可以使用:
{%set tmp = include('variables.twig') %}
{{restore('test')}}
{{foo}}
Final note
我不习惯JavaScript的twig端口,但看起来你仍然可以扩展它,这就是你的选择:)
I have container.twig including component.twig and passing an object called 'mock'.
In container.twig:
{% set mock = {
title : "This is my title"
}
%}
{% include 'component.twig' with mock %}
This is working fine but I want to move the mock data to its own file. This isnt working:
Container.twig
{% include 'component.twig' with 'mock.twig' %}
In mock.twig
{% set mock = {
title : "This is my title"
}
%}
Im using gulp-twig but it works like standard twig in most respects. https://github.com/zimmen/gulp-twig
解决方案 The problem
Twig context is never stored in the template object, so this will be very difficult to find a clean way to achieve this. For example, the following Twig code:
{% set test = 'Hello, world' %}
Will compile to:
<?php
class __TwigTemplate_20df0122e7c88760565e671dea7b7d68c33516f833acc39288f926e234b08380 extends Twig_Template
{
/* ... */
protected function doDisplay(array $context, array $blocks = array())
{
// line 1
$context["test"] = "Hello, world";
}
/* ... */
}
As you can see, the inherited context is not passed to the doDisplay method by reference, and is never stored in the object itself (like $this->context = $context
). This deisgn allow templates to be reusable, and is memory-friendly.
Solution 1 : using global variables
I don't know if you are aware of Global Variables in Twig. You can do a bunch of hacks with them.
The easiest usage is to load all your globals inside your twig environment.
$loader = new Twig_Loader_Filesystem(__DIR__.'/view');
$env = new Twig_Environment($loader);
$env->addGlobal('foo', 'bar');
$env->addGlobal('Hello', 'world!');
Then, you can use {{ foo }}
and {{ Hello }}
in your whole application.
But there are 2 problems here:
As you're trying to load variables from twig files, I assume you have lots of variables to initialize depending on your feature and don't want to load everything all time.
you are loading variables from PHP scripts and not from Twig, and your question want to import variables from a twig file.
Solution 2 : using a Twig extension
You can also create a storage extension that provide a save
function to persist some template's context somewhere, and a restore
function to merge this stored context in another one.
proof_of_concept.php
<?php
require __DIR__.'/vendor/autoload.php';
class StorageTwigExtension extends Twig_Extension
{
protected $storage = [];
public function getFunctions() {
return [
new Twig_SimpleFunction('save', [$this, 'save'], ['needs_context' => true]),
new Twig_SimpleFunction('restore', [$this, 'restore'], ['needs_context' => true]),
];
}
public function save($context, $name) {
$this->storage = array_merge($this->storage, $context);
}
public function restore(&$context, $name) {
$context = array_merge($context, $this->storage);
}
public function getName() {
return 'storage';
}
}
/* usage example */
$loader = new Twig_Loader_Filesystem(__DIR__.'/view');
$env = new Twig_Environment($loader);
$env->addExtension(new StorageTwigExtension());
echo $env->render('test.twig'), PHP_EOL;
twig/variables.twig
{% set foo = 'bar' %}
{% set Hello = 'world!' %}
{{ save('test') }}
twig/test.twig
{% include 'variables.twig' %}
{{ restore('test') }}
{{ foo }}
Note: if you only want to import variables without actually rendering what's inside twig/variables.twig, you can also use:
{% set tmp = include('variables.twig') %}
{{ restore('test') }}
{{ foo }}
Final note
I'm not used to the JavaScript twig port, but it looks like you can still extend it, that's your go :)
这篇关于包含一个树枝文件并从一个单独的文件传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!