本文整理汇总了PHP中Twig_Environment::setLoader方法的典型用法代码示例。如果您正苦于以下问题:PHP Twig_Environment::setLoader方法的具体用法?PHP Twig_Environment::setLoader怎么用?PHP Twig_Environment::setLoader使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Twig_Environment
的用法示例。
在下文中一共展示了Twig_Environment::setLoader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: render
/**
* Render a string
*
* A special option in the $locals array can be used to define the Twital adapter to use.
* The array key is `__twital-adapter`, and the value is an instance of `\Goetas\Twital\SourceAdapter`
*
* @param string $template The template content to render
* @param array $locals The variable to use in template
* @return null|string
*
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Runtime
* @throws \Twig_Error_Syntax
*/
public function render($template, array $locals = array())
{
if (array_key_exists('__twital-adapter', $locals)) {
$this->stringLoader->addSourceAdapter('/.*/', $locals['__twital-adapter']);
}
// Render the file using the straight string.
$this->environment->setLoader($this->stringLoader);
return $this->environment->render($template, $locals);
}
示例2: addDir
/**
* add a directory to template search dirs
* @param string $directory
* @param bool|true $primary
* @return ITemplateEngine|void
*/
public function addDir($directory, $primary = true)
{
if ($primary) {
$this->twigLoader->prependPath($directory);
} else {
$this->twigLoader->addPath($directory);
}
$this->twigEnv->setLoader($this->twigLoader);
return $this;
}
示例3: setUp
protected function setUp()
{
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not installed.');
}
$this->mediaStorage = new FilesystemMediaStorage(__DIR__ . "/fixtures/stored", "/project/web/media/", "http://localhost/project/web/media/", FALSE);
$this->twig = new \Twig_Environment();
$this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__ . '/fixtures/templates'));
$this->twig->addExtension(new TwigMediaStorageExtension($this->mediaStorage, TRUE));
}
示例4: setUp
protected function setUp()
{
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not installed.');
}
$this->mapper = $this->getMock('Midgard\\CreatePHP\\RdfMapperInterface');
$xmlDriver = new RdfDriverXml(array(__DIR__ . '/../../Metadata/rdf-twig'));
$this->factory = new RdfTypeFactory($this->mapper, $xmlDriver);
$this->twig = new \Twig_Environment();
$this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__ . '/templates'));
$this->twig->addExtension(new CreatephpExtension($this->factory));
}
示例5: _run
/**
* 表示するためのファイルを組み立て
*
* @access protected
*/
protected function _run()
{
// 引数のファイルパスをview scriptまでのパスとcontroller/action.:suffixの形式に分断する
$paths = $this->_parseFilePath(func_get_arg(0));
// view scriptまでのパスを指定
$this->_twig->setLoader(new Twig_Loader_Filesystem($paths["path"]));
// 登録した配列の0番目のViewScriptをテンプレートとして読み込み
$template = $this->_twig->loadTemplate($paths["file"]);
// APPLICATION_ENVをTwigから利用出来るようにするため変数に入れておく
$this->env = APPLICATION_ENV;
echo $template->render($this->getVars());
}
示例6: render
/**
* @param \Twig_Environment $env
* @param $context
* @param FormView $form
* @param string $formVariableName
* @return array
*/
public function render(\Twig_Environment $env, $context, FormView $form, $formVariableName = 'form')
{
$this->formVariableName = $formVariableName;
$this->formConfig = new FormConfig();
$this->context = $context;
$this->env = $env;
$tmpLoader = $env->getLoader();
$env->setLoader(new \Twig_Loader_Chain([$tmpLoader, new \Twig_Loader_String()]));
$this->renderBlock($form);
$env->setLoader($tmpLoader);
return $this->formConfig->toArray();
}
示例7: transform
/**
* {@inheritdoc}
*/
public function transform($text)
{
// Here we temporary changing twig environment loader to Chain loader with Twig_Loader_Array as first loader, which contains only one our template reference
$oldLoader = $this->twig->getLoader();
$hash = sha1($text);
$chainLoader = new \Twig_Loader_Chain();
$chainLoader->addLoader(new \Twig_Loader_Array(array($hash => $text)));
$chainLoader->addLoader($oldLoader);
$this->twig->setLoader($chainLoader);
$result = $this->twig->render($hash);
$this->twig->setLoader($oldLoader);
return $result;
}
示例8: render
/**
* @param \Twig_Environment $env
* @param array $context
* @param FormView $form
* @param string $formVariableName
*
* @return array
*/
public function render(\Twig_Environment $env, $context, FormView $form, $formVariableName = 'form')
{
// remember current loader
$originalLoader = $env->getLoader();
// replace the loader
$env->setLoader(new \Twig_Loader_Chain(array($originalLoader, new \Twig_Loader_String())));
// build blocks
$builder = new DataBlockBuilder(new TwigTemplateRenderer($env, $context), $formVariableName);
$result = $builder->build($form);
// restore the original loader
$env->setLoader($originalLoader);
return $result->toArray();
}
示例9: twig_template_from_string
/**
* Loads a template from a string.
*
* <pre>
* {{ include(template_from_string("Hello {{ name }}")) }}
* </pre>
*
* @param Twig_Environment $env A Twig_Environment instance
* @param string $template A template as a string
*
* @return Twig_Template A Twig_Template instance
*/
function twig_template_from_string(Twig_Environment $env, $template)
{
$name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false));
$loader = new Twig_Loader_Chain(array(new Twig_Loader_Array(array($name => $template)), $current = $env->getLoader()));
$env->setLoader($loader);
try {
$template = $env->loadTemplate($name);
} catch (Exception $e) {
$env->setLoader($current);
throw $e;
}
$env->setLoader($current);
return $template;
}
示例10: setUp
protected function setUp()
{
if (!class_exists('Twig_Environment')) {
$this->markTestSkipped('Twig is not installed.');
}
$this->am = $this->getMock('Assetic\\AssetManager');
$this->fm = $this->getMock('Assetic\\FilterManager');
$this->valueSupplier = $this->getMock('Assetic\\ValueSupplierInterface');
$this->factory = new AssetFactory(__DIR__ . '/templates');
$this->factory->setAssetManager($this->am);
$this->factory->setFilterManager($this->fm);
$this->twig = new \Twig_Environment();
$this->twig->setLoader(new \Twig_Loader_Filesystem(__DIR__ . '/templates'));
$this->twig->addExtension(new AsseticExtension($this->factory, array(), $this->valueSupplier));
}
示例11: boot
public function boot()
{
$configPath = __DIR__ . '/../config/form.php';
$this->publishes([$configPath => config_path('form.php')], 'config');
if ($this->app->bound(\Twig_Environment::class)) {
/** @var \Twig_Environment $twig */
$twig = $this->app->make(\Twig_Environment::class);
} else {
$twig = new \Twig_Environment(new \Twig_Loader_Chain([]));
}
$loader = $twig->getLoader();
// If the loader is not already a chain, make it one
if (!$loader instanceof \Twig_Loader_Chain) {
$loader = new \Twig_Loader_Chain([$loader]);
$twig->setLoader($loader);
}
$reflected = new \ReflectionClass(FormExtension::class);
$path = dirname($reflected->getFileName()) . '/../Resources/views/Form';
$loader->addLoader(new \Twig_Loader_Filesystem($path));
/** @var TwigRenderer $renderer */
$renderer = $this->app->make(TwigRendererInterface::class);
$renderer->setEnvironment($twig);
// Add the extension
$twig->addExtension(new FormExtension($renderer));
// trans filter is used in the forms
$twig->addFilter(new \Twig_SimpleFilter('trans', 'trans'));
// csrf_token needs to be replaced for Laravel
$twig->addFunction(new \Twig_SimpleFunction('csrf_token', 'csrf_token'));
$this->registerBladeDirectives();
}
示例12: getEnv
private function getEnv()
{
$env = new \Twig_Environment();
$env->addTokenParser(new TwigJsTokenParser());
$env->setLoader(new \Twig_Loader_String());
return $env;
}
示例13: twig_template_from_string
/**
* Loads a template from a string.
*
* <pre>
* {% include template_from_string("Hello {{ name }}") }}
* </pre>
*
* @param Twig_Environment $env A Twig_Environment instance
* @param string $template A template as a string
*
* @return Twig_Template A Twig_Template instance
*/
function twig_template_from_string(Twig_Environment $env, $template)
{
static $loader;
if (null === $loader) {
$loader = new Twig_Loader_String();
}
$current = $env->getLoader();
$env->setLoader($loader);
try {
$template = $env->loadTemplate($template);
} catch (Exception $e) {
$env->setLoader($current);
throw $e;
}
$env->setLoader($current);
return $template;
}
示例14: renderString
/**
* @param string $string
* @return string
*/
public function renderString($string)
{
// no rendering if empty
if (empty($string)) {
return $string;
}
// see Twig\Extensions\Twig_Extension_StringLoader
$name = '__twig_string__';
// get current loader
$loader = $this->environment->getLoader();
// set loader chain with new array loader
$this->environment->setLoader(new Twig_Loader_Chain(array(new Twig_Loader_Array(array($name => $string)), $loader)));
// render string
$rendered = $this->environment->render($name);
// reset current loader
$this->environment->setLoader($loader);
return $rendered;
}
示例15: parseString
/**
* Sets the parser for the environment to Twig_Loader_String, and parsed the string $string.
*
* @param \Twig_Environment $environment
* @param array $context
* @param string $string
* @return string
*/
protected function parseString(\Twig_Environment $environment, $context, $string)
{
try {
$environment->setLoader(new \Twig_Loader_String());
return $environment->render($string, $context);
} catch (\Exception $exc) {
return null;
}
}