Access to a block view in a paragraph in Drupal - php

I created a paragraph in drupal which has a reference to a block.
In the preprocess I did :
$variables['bloc_video'] = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($bp);
And in my twig file I just called it like that :
{{bloc_video}}
The view is unfortunately not correct.
I want my preprocess to call my block view in another twig file. How can I do that?
Thanks a lot

I suggest using Twig Tweak module (it is very useful also for other cases). Here you can find more about including blocks in twig with Twig Tweak module.
In you case you can use:
{{ drupal_entity('block', 'block_content') }}
However, if you would still like to do it programmatically, check this answer.

Related

How to replace <?php echo with curly brace({{ }})

I want to cleanup my php file. I really like the way how blade template use curly brace to display php variables {{ $user }} https://laravel.com/docs/5.8/blade#displaying-data
Is there a way to use a functionality like that without using blade template. Using <?php echo $user ?> get's very repetitive.
This is not possible in PHP without using a template engine because it is a different syntax. Anyway in my opinion sometimes it is not the best idea to use a template engine because PHP is already kind of a template engine.
All what you seem to care about is the short syntax of echoing a variable in blade:
{{ user }}
This is as short as the syntax the PHP template engine itself provides:
<?= $user ?>
Of course Laravel (and similar) has a good reason to use template engine like blade - in order to seperate controller, logic and view (such that the view can be made by non-programmer designers for instance).
Update (thx to comments): The codes provided above do not exactly do the same as template engines (as an additional layer) may take care of e.g. escaping variables, which adds safety to the code.
As the OPs' code has already been built with echo and he is just searching for a replacement I did not mention this, but its a notable difference that you have to take care of some things yourself if not using one.

Append onto current URL with Twig?

I'm currently making a simple file hosting script using Slim, Twig, and PHP. Right now I'm trying to append onto the current URL using Twig but am not sure how to do this. I've tried /panel/{{ newURL }} but it always just redirects me to /panel/newurl. I need to be able to dynamically update this URL.
For example, if I want to go to /tests on /panel/core/ I need to be able to append that onto the current URL. Does Twig offer a way to do this? Thanks.
Do you use Twig to render your paths? I think your application should always be able to resolve the paths, so also let the application itself render them ;-)
If that would help; you can add the parameters of the current request to the path rendering:
{{ path('yourpath', app.request.query.all|merge({'myparam': 'value'})) }}
you can use the Built-in parameter in twig {{ app.request.uri.path }}
You'll want to ensure that you've included all route and query string parameters in the URL you're appending to.
Here's an example of appending foo=bar to the current URL:
{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge(app.request.query.all)|merge({'foo': 'bar})) }}
I ended up just passing 'url' => $request->getUri()->getPath() in my view parameters. I think this is a pretty decent method because there are other methods you can call on getUri() to get different results depending on what you want to do.

With Mustache PHP, loading partials but not parsing tags

I have a Mustache template which includes some partials. I want to load the partial into a string in PHP, with the contents of the partials included in the string. But I don't want the template to be parsed.
eg, if I have this Mustache template, myTemplate.mustache:
{{> partials/myPartial }}
{{ my_text }}
and this is partials/myPartial.mustache:
{{ my_header }}
then I want to load myTemplate.mustache and have a PHP variable containing:
{{ my_header }}
{{ my_text }}
I can see how to get the contents of the template using new Mustache_Loader_Filesystem()->load() but that doesn't include the contents of the partial.
You can't see a way to do that, because it's not possible. Mustache itself never even deals with templates merged together like that, so there's no reason it would expose an API for getting them.
Because of the nature of partials, it's not as simple as replacing a partial tag with the contents of that template. For example, partials don't inherit delimiter changes and pragmas from templates which include them, so that would have to be addressed. Partials are also indented to the level of the tag which includes them, so replacing a partial tag with the contents of the partial would have to do this as well.
I supposed if you really want it, you could load the template and partials, then use the Mustache Tokenizer to find partials tags in the template source then replace them (recursively) with the contents of the associated partials. Then you'd have to figure out how to change delimiters at the start and end of the inlined partials (or disallow delimiter changes entirely, which you could do by throwing an exception if you encounter one while processing the parsed template). And I can't think of a way to remove pragmas once they're added, so you would either have to disallow pragmas, or ensure that all inlined partials were compatible with whatever pragmas were being used in parent templates.
It'd be a fair amount of work, to say the least.

How can I use a Smarty template from a Smarty template?

I've run into a funny problem. I need to use Smarty templates within a Smarty template.
Here is why. I use the same templates for various wiki websites, and each website has its own configuration. The configuration contains parts for the main template (such as changed titles and headings, etc).
Here is a simplified example. I've a file topic-list.template.html that's shared across all websites:
<div id="topics">
<h1>{$h1}</h1>
...
</div>
As you can see, this template file contains an <h1> tag that can be customized for each website.
Then for each of the websites I've a configuration file that looks like this (simplified):
$config = [
"h1-titles" => [
"topics" => "Showing Topics in {\$category}"
]
];
As you can see the configuration file contains a Smarty template.
So when I render the topic-list.template.html file, I've to render the $config['h1-titles']['topics'] first through $smarty->fetch("string":$config['h1-titles']['topics']), and then assign it to h1 Smarty variable. My simplified code looks like this:
$h1_tag = $smarty->fetch("string":$config['h1-titles']['topics']);
$smarty->assign('h1', $h1_tag);
$smarty->display('topic-list.template.html');
I wonder if I could somehow insert the $config['h1-titles']['topics'] in the topic-list.template.html file automatically and then it have all rendered in one go?
Please have a look at the docs on String Template Resources. You will immediately notice that your $smarty->fetch('string:…') approach can also be done within a template: {include file="string:…"}
I believe that {eval} tag may help you:
{eval} is used to evaluate a variable as a template. This can be used for things like embedding template tags/variables into variables or tags/variables into config file variables.
http://www.smarty.net/docs/en/language.function.eval.tpl

Is there a template system like Twig that uses PHP syntax?

I would like to use a template system like Twig (specifically the block functionality) but in plain PHP.
For example, I found http://phpsavant.com/docs/ but it seems that doesn't support blocks or anything similar.
Edit
I found something that appears to have block syntax with regular PHP code: http://phpti.com/
The templating language you are looking for is called PHP!
There is one included in the Laravel framework called Blade.
You can mix plain old PHP with the Blade templating syntax, where {{...}} also translates to <?=...?> or <?php echo ... ?>
Also has blocks you know in Twig, but they are called sections.
#section('heading')
{{ strtoupper("I'm not shouting") }}
#show
<?= strtolower('Shhh!'); ?>
This is under the Illuminate\View namespace - See on GitHub, and can be downloaded with Composer as it is also registered on Packagist - just at the following to your project's composer.json.
{
"require": {
"illuminate/view": "4.*"
}
}
I'm not certain at this point about how you'd attempt to render a template from a custom project. If I find out, I'll update my answer.
Symfony (which uses Twig by default) also has a php-based template system that works very much like Twig. It has template inheritance and uses 'slots', which are equivalent to Twig's blocks. It's a stand-alone library that can be used outside of the full Symfony framework.
http://symfony.com/doc/2.0/cookbook/templating/PHP.html
PHP is a templating language(yes it is) , blocks can be implemented with buffers :
<?php
ob_start();
?>
this the content of the block for <?= date("Y-m-d") ?>
<?php
$content = ob_get_clean();
then in the main layout echo the content of the blocks :
echo $head;
echo $content ;
// ....
in fact that is what most template engines are using.
using template libs have huge advantages though ( caching , escaping by default ,etc ... )
As far as I understand use off Blade for views is optional in Laravel. You can use file name like view.php instead of view.blade.php and use plain old PHP syntax there. Only catch is you cannot have both view.php and view.blade.php as they both respond to
return View::make('view);

Categories