I am trying to use template_from_string as stated on
http://twig.sensiolabs.org/doc/functions/template_from_string.html
How can I do that from Silex? I see that the Twig/Exteion/StringLoader.php file is there. Here is the code I've tried
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$twig->addExtension(new MarkdownExtension());
$twig->addExtension(new Twig_Extension_StringLoader());
return $twig;
}));
But when I try to use it like
return $app['twig']->template_from_string(
"The is the {{ title }}",
array('title' => 'Hello')
);
It generates following error
Fatal error: Call to undefined method Twig_Environment::template_from_string()
What I am trying to do is fetch the template content from DB or another file, then render it with Twig instead of using a template file, so I can combine several section templates into the main template. Or if there a better way?
Please note that I already know how to use insert within the template file like
{% include 'home-section.html.twig' %}
but this won't solve my problem because it can not fetch the content data to be parsed automatically.
Thank you.
Just had to create another twig object
$loader = new Twig_Loader_String();
$twig = new Twig_Environment($loader);
echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien'));
Related
I am using twig seperately, not from within Symfony.
I have base layout base.html. Other layouts extend that one and that seems to work fine.
If I have a controlleraction BlogPosts which passes an array of blogposts, I render the blog layout, which extend from the base layout to see that content. That works as intended
Now, I would like to pass variables to the base template, so it is visible on every page. How can I do this?
I found this article, but that mentions it for symfony. On its own, twig does not have the render function.
If you are using twig without symfony, I assume your project directory structure is similar like this:
project
- templates
- base.html.twig
- index.html.twig
- vendor
- index.php
So, your index.php code should be like:
require_once 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('templates', getcwd());
$twig = new Twig_Environment($loader);
echo $twig->render('index.html.twig', array('name' => 'Hello'));
So, if you want pass some variable to your base template, twig has method to add value for using globally
require_once 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('templates', getcwd());
$twig = new Twig_Environment($loader);
$twig->addGlobal('title', 'HomePage');
echo $twig->render('index.html.twig', array('name' => 'Hello'));
I hope this help
I'm using Laravel basic authentication but I am really struggling to use the Auth object in the views.
There are good examples using Blade, but I am using Twig (twig bridge) and cannot solve this.
I can vardump(Auth::user()->name) in my controller in regular php but how do I get the same in the twig file (my view file)?
How do I do something like;
{% if auth.guest %}
Or;
{{ Auth.user().name }}
I've tried so many different ways but just get nothing.
This has to do with the way twig works, accessing static variables /classes or static methods on classes is impossible without adding a function to the Twig Environment.
You can register a function like so:
$twig = new Twig_Environment($loader);
$twig->addFunction('staticCall', new Twig_Function_Function('staticCall'));
function staticCall($class, $function, $args = array())
{
if (class_exists($class) && method_exists($class, $function))
return call_user_func_array(array($class, $function), $args);
return null;
}
Then i think you should be able to do:
{% if staticCall('Auth', 'guest') %}
Hi there stranger, why don't you login?
{% endif %}
I took the code from this answer: twig template engine, using a static function or variable
Hello so I have a simple code here that will render home.html using slim framework and twig. Here's the codes:
In my index.php file:
require_once 'vendor/autoload.php';
$app = new \Slim\Slim([
'debug' => true,
'templates.path' => 'app/views'
]);
$app->view = new \Slim\Views\Twig();
$app->view->setTemplatesDirectory("app/views");
$view = $app->view();
$view->parserOptions = ['debug' => true];
$view->parserExtensions = [new \Slim\Views\TwigExtension()];
$app->get('/home', function () use ($app) {
$app->render('home.html');
});
$app->run();
Here is the base.html template:
And my home.html:
{% extends "base.html" %}
{% block content %}
Some content here
{% endblock %}
My question is, since the only rendered part is the home.html, what if I want some data loaded in my base template? Like this..
So that I won't have to repeat it on every page I render. Is that possible on a base template? Thank you in advance.
Also, this is what I followed to install twig in slim.
I think the best answer is given here. There the answerer say "write a Twig custom function" to load dynamically data in your views.
So you would be able to write your own PHP, can inject your DB and use this along with that templating engine.
I'm writing a website using Slim Framework and Twig.
The sidebar on the website will have dynamically created links and i want to get the links from the database/cache every time a page is rendered, i can do this in each controller action but i would like to do this in my base template so i don't have to get and render the data manually in every controller action.
I have looked through the twig documentation and I haven't seen anything that could be of use besides the include function/tag but i don't see how i could get the data easily.
Is my only option to write an twig extension to do this or is there an easier way?
First of all: templates usually shouldn't contain any type of bussines logic but only render the data you provide it.
However you could write a custom twig function and use that to aquire the menu data from the DB in order to render it in your base template.
Alternativeley you could write some Slim middleware that aquires the data which might be able to inject the data into the template.
Hope that helps.
After reading Anticoms answer i was able to do it by writing a Twig extension.
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('render', array($this, 'render'))
);
}
public function render($template, $controller, $action) {
$app = Slim::getInstance();
return $app->$controller->$action($template);
}
Now i can simply write
{{ render('Shared/sidebar.twig', 'Controller', 'Index') }}
Into my twig template to render the template with the data i want.
Note that my render() function uses slim dependency injection to instanciate the controller at runtime.
In php I can use in my template:
use My\WebBundle\Classes\Util;
How can I put the same in Twig tempate?
Thanks.
You can't create objects in twig template, but you can pass them in twig template from controller:
public function postsAction() {
return $this->render('AcmeBlogBundle:User:posts.html.twig', array(
'util' => new My\WebBundle\Classes\Util(),
));
}
And in twig template can use it:
{{ util.anyProperty }}
You don't want PHP in your twig template. All you need to do is tell twig what variables you use. It's part of the MVC.
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller