I am planning on converting a website to using MVC and php. I am planning on using the Twig template engine so that I can remove all code from the page templates, but I have hit on a problem, and that is with my menu. I only want a certain level of user to access each menu item.
I have a function which I call which returns true if a level is allowed. So for instance if you have a level of purchase you will be allowed to see Menu Item 1, but not allowed to see Menu Item 2. For instance Sales would not be able to see the first two menu items.
I know that you cannot call functions or classes in Twig.
eg (my function is in a class called logon, but that shouldn't matter)
<?php if ($logon->user_access('purchase','accountant','poweruser')):?>
Menu Item 1
<?php endif;?>
<?php if ($logon->user_access('accountant','poweruser')):?>
Menu Item 2
<?php endif;?>
<?php if ($logon->user_access('sales')):?>
Menu Item 2
<?php endif;?>
The question is how do I make a template in twig with this functionality without using php? I am totally new to using a template engine.
edit... @Yoshi
Ok I include twig in my index.php with
require_once dirname(__DIR__).'/vendor/Twig/vendor/autoload.php';
Then whenever I want to render the page I call a function from my core/view
public static function renderTwig($template,$args =[])
{
static $twig = null;
if($twig === null){
$loader = new \Twig_Loader_Filesystem('../App/Views');
$twig = new \Twig_Environment($loader);
echo $twig->render($template, $args);
}
}
Then in my controller I call
View::renderTwig('Home/index.html',[
'css'=> $css,
'name' => 'Dave',
'colours' => ['red','green','blue']
]);
I set this all up from a tutorial I have been following called "Create php mvc project from scratch".
PS. I suppose in my renderTwig function I could send in variables which are used on every page and add it to the arg array for the page. That way I wouldn't need to add authentication to every page.
Edit: I am making a small amendment here. Even though Yoshi's answer is correct. I had a small problem which I have now solved, and I thought I would share this. In the twig template I am using it like this
{% if user_access('sales','purchase') %}Do something{% endif %}
The problem is that I don't know the number of items that are coming in so I need to use args, and this caused me a small problem, as the following line sent the string to my function, but only one item.
return $this->logon->user_access($roles);
The problem with func_get_args is that you cannot pass it to another function. It can only be read in the function that you send it to. So my small amendment was this.
return $this->logon->user_access($args = func_get_args());
Now the problem here is that it sends an array instead, but my php routines were set up to use args, and I wanted to use the same function in php that I used in twig. So what I did was to check if an array came in, I just looped through the array, but if a non array came in I checked the args. eg
public function user_access($levels)
{
$bool=false;
if(is_array($levels)):
foreach($levels as $item):
if($this->userlevel($item)==true $bool=true;
endforeach;
else:
foreach(func_get_args() as $arg):
if($this->userlevel($arg)==true $bool=true;
endforeach;
endif;
return $bool;
}
So thankyou Yoshi
$logon
variable to your template – DarkBee$logon
to the view. – Yoshi