Rendering multiple pages with Twig - php

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']);
?>```

Related

Twig render other controller from base view

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

October CMS Component Load on Layout

Im new to October CMS
I read the documentation and its states that components can be used inside a layout on the PHP Section (https://octobercms.com/docs/cms/layouts)
i want to create a component that will be used as configuration file for my theme, declaring global variables, that will be used on all pages, but will also be used for all layouts i will create. but i cant find how to do it via code. Like include a file. i want this to used this parameters inside the PHP Section of the layout and the pages.
If components is not the best way, can you ppl sugest me what is the best way
I did before search a lot to find some way that I can share variables to all my layouts and pages but couldn't find anything.
So I tried my own trick and it worked.
In your frontend you must have header.htm partial. so in the code section in it write a onStart() function and set your global variables so you can access them from any layout or page which includes the header partial.
For example in your header.htm code section:
function onStart() {
$this['my_var'] = ['name' => 'Ahmed', 'age' => 17];
}
That way you can access my_var variable wherever you want in all your layouts and pages which header.htm partial is included.

Accessing other pages with Twig

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

Set Custom Views for Slim after it's been instantiated

I'm working on a very old legacy website(think PHP 4 constructs). Slowly but surely I'm refactoring most of the code. Since it's not really a huge amount of code I decided to use Slim.
I'm using Custom Views, and probably in the near future I'll go for a tempting engine, probably Twig.
Long story short, I'm autoloading the Custom Views folder to have them ready wherever. As far as my understanding of Slim goes, to use the Custom View I need to do the following:
$app = new Slim(array(
'view' => 'CustomView'
));
This is all nice and easy to use, however I'd like to have(if that's even possible) Slim loaded as a property of a some base class(along with other things) and have other classes extend that one. Clearly this presents problems since I can only have a single Custom View.
Is there some other way of passing in the Custom View to Silm, after it's instantiated?
Possibly something like:
$app = new Slim();
$app->setView(Custome View Goes Here);
I couldn't find anything relevant on Google.
Following should work. Example taken from documentation.
The View class or instance used by the Slim application. To change this > setting after instantiation you need to use the Slim application's view() method.
// During instantiation
$app = new \Slim\Slim(array(
'view' => new \My\View()
));
// After instantiation
$app->view(new \My\View());

Can I include a view in Kohana 3 that is not within `application/views`?

I am building a staff area for a website, which is completely different to the main brochure style site.
I have 2 Kohana systems setup. I realise they can both share the same system and modules folder.
Now, with the second one, I want to make the main template view a view in a different folder.
I tried this in my base controller
$this->template = DOCROOT . '../~new2/application/views/template.php';
But Kohana is looking for it in its own views folder as evident by the error I received. I even put a var_dump(file_exists($this->template)); // true to be sure it was finding the correct file.
Is there a way to add a template file that is not within the views folder, without hacking the core Kohana code (and if I'm lucky not extending and overloading the view class).
It's quite hacky, but knowing that it is looking in the views folder, you can point to your view like so.
$this->template = '../../../~new2/application/views/template';
$path = APPPATH . 'views/' . $this->template;
var_dump(realpath($path)); // proper path
var_dump(file_exists($path)); // true
Yeah, it's ugly. But it works.

Categories