Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Shortcodes in twig file?

Started by Muut Archive 11 years ago · 5 replies · 1211 views
11 years ago

Hi,
is it possible to use shortcodes in a html.twig file?
Should I use both lines (or only one of them) as described in your docs?

TWIG
{{% embed page="/path-or-slug" modular=false|true template="name" %}}
{{% embed "/path-or-slug" modular=false|true template="name" %}}

For grav/twig beginners it's not so easy to understand.
Maybe it's possible to give a full example with result for the cookbook Plugins section?
Thanks!

11 years ago

Hi @arank,

Shortcodes cannot directly be mixed with Twig content. However, it implements a Twig function called shortcodes you can use in your Twig files, e.g.

TWIG
{{ shortcodes("{{% embed '/path-to-your-page' %}}") }}

But be warned! Some shortcodes may not work as expected, when using them in this way.

If you really like to embed content from other pages from a twig file, then I strongly recommend to use the following Twig snippet

TWIG
{{ pages.find('/path-to-your-page').content }}
11 years ago

Hi, thanks. Exactly what I was looking for.

11 years ago

I have a modular page. So the path should be:
/usr/pages/01.home/_test
But the content isn't shown???
Wrong path?

11 years ago

Hi @arank,

yeah that's most possibly the wrong path. Usually you have to insert the path you can see from the browser for a specific page, i.e. for a page you can access it via

TXT
http://mysite.com/my-super-page

you have to write

TWIG
{{ pages.find('/my-super-page').content }}

and for modular content

TWIG
{{ pages.find('/my-super-page/_modular').content }}

(in your case the slug is maybe /home/_test or just /_test). I haven't tried it, but it is really difficult to tell you the correct path, when I don't know how your folder structure looks like.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1332 9 years ago
Archive · by Muut Archive, 9 years ago
2 923 9 years ago
Archive · by Muut Archive, 9 years ago
2 4053 9 years ago
Archive · by Muut Archive, 9 years ago
1 2931 9 years ago
Archive · by Muut Archive, 9 years ago
3 1109 9 years ago