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
Related
I am building a website using Twig as my template engine (without Symfony or any other frameworks). How do I go about rendering several different PHP pages?
So far I only have one page – index.php – which in my project is supposed to handle the homepage. If I want to add more (for instance about.php, article.php, etc.), what would be the best approach to do that? Do I need to create those other pages and use the same code as below, changing only the template file name, or is there some other way to handle the routing?
This is my index.php:
require __DIR__ . '/vendor/autoload.php';
$loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates');
$twig = new \Twig\Environment($loader);
echo $twig->render('index.html.twig', ['title' => 'Project Title']);
?>```
I've started learning Twig today and wanted to know how I would redirect to another page in my template.
This is my index.php where I also load my homepage.
<?php
require '../vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('../recourses/views');
$twig = new Twig_Environment($loader);
echo $twig->render('pages/home.twig', array(
'project_title' => getProjectTitle()
));
Now my question is: How could I reach this page with an ?
I can't just use <a href ="pages/home.twig"> since it will show the code instead of the page itself.
I've tried searching but couldn't find my sollution.
Hope anyone can explain this to me.
If I unterstand, you are not using Symfony Standard Edition, but some Symfony components + Twig ?
In that case, Twig is only a a templating engine and Symfony functions are missing.
The path function (ShinDarth answer) is provided by a twig extension defined in the Symfony Standard Edition :
http://symfony.com/doc/current/reference/twig_reference.html#path
All the Symfony Standard Edition function are defined at the same page :
http://symfony.com/doc/current/reference/twig_reference.html
Using Twig like you do, you can only pass the path as variable and render it as {{ path_variable }}
Supposing that you have your route named my_route,
then via twig you simple do:
Link to my_route
Documentation: http://symfony.com/doc/current/book/templating.html#linking-to-pages
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 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'));
my problem is fairly when I call a view helper from view script it can't be called
although I added properly all information path to the config file via this line:
resources.view.helperPath.ZF_View_Helper_="ZF/View/Helper/"
also I registered the helper in bootstrap file
function _initViewHelpers(){
$view = new Zend_View();
$view->addHelperPath('ZF/View/Helper','ZF_View_Helper');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}
but in vain it still printing out this error message:
Application error
Exception information:
Message: Plugin by name 'OutputHelper' was not found in the registry; used paths:
Zend_View_Helper_: Zend/View/Helper/
it doesn't include the custom view helper path as expected ;
the path of the view helper is: library/ZF/View/Helper/OutputHelper.php
can you do this:
in view script
$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
var_dump($this === $view);
var_dump($view->getHelperPaths());
exit;
I think your view instance are replaced at some point.
May be module's bootstrap have view resource?
Or it can be other obvious mistake. So obvious so you'll never think of it
btw remove that _initViewHelpers method. Zend_Application_Resource_View work just fine for that.
And if you use this method, use it correctly, eg:
$this->bootstrap('view');
$view = $this->getResource('view');
//whatever