Home Page Blog
Created 7 years ago by psaunders

I'm just working out how to perform a relatively common request: How do I make make the home page show either (A) the blog module or (B) a couple of posts? Back with the details in a bit.

Update 1 The docs and various stock theme parts show examples that have something along the lines of:

{% posts = entries('posts').paginate() %}
{% for post in posts %}
    <p>
        {{ post.title }}
    </p>
{% endfor %}
{{ posts.links|raw }}

There is also an example that has posts in this format, which looks like some sort of eager loading but I can't find the entries() function in the code so it must be parsed into something with a different name.

{% for category in entries('posts', 'categories').get() %}
   <li>
      {{ category.slug }}
</li>
{% endfor %}

You can't just paste this markdown into the WYSIWYG editor or the page will 500, so you probably have to create a special "home page" .twig file or perhaps a layout inside pyro.

Update 2

Doesn't look like you can put code into the database or the site will 500 with "Twig_Error_Syntax: Unknown "posts" tag" which I guess is a limitation of using a templating engine that works with files. I do wonder if the layouts for page "types" could be cached as .twig files in storage?

ryanthompson  —  7 years ago

Ya the WYSIWYG might format some things kinda funny as a sanitary action (which results in 500 I assume in your case). I would never advise putting code into the editor. You can use grid to safely allow adding HTML to a page or even as a partial so the client never sees it. But a homepage page type would be another good option.

Entries is a core function for the streams platform that let's you access any stream data you want: https://pyrocms.com/documentation/streams-plugin/latest#usage/entries

PS entries('posts') is short for entries('posts', 'posts'). Since they're the same you can just provide the namespace since the slug is the same.

ryanthompson  —  7 years ago

And you can route the home page to the posts module by adding a route like:

Route::get('/', 'Anomaly\PostsModule\Http\Controller\PostsController@index');

Just like the module does basically. Since the /resources/{REF}/routes.php file is loaded last you can create it / add this route to them to override all prior defined routes.

psaunders  —  7 years ago

Ok so the entries( ) arguments are pretty much

entries("namespace","table")

And the system does cache layouts and code in the storage directory. For me I created a page type called "code" that has a field type that is a twig editor which i pasted the snipet of code from the top into and out came the errors:

[2017-05-04 02:05:52] local.CRITICAL: Symfony\Component\Debug\Exception\FatalErrorException: Method Anomaly\EditorFieldType\EditorFieldTypePresenter::__toString() must not throw an exception, caught Twig_Error_Syntax: Unknown "posts" tag in "/Users/admin/www/websitename/storage/streams/default/pages/home_page_pages/3/code.twig" at line 1. in /Users/admin/www/websitename/vendor/twig/twig/lib/Twig/Extension/Core.php:0
Stack trace:
#0 {main} {"identification":{"id":"f998960b-d8c0-4aa9-91e2-28f391ffae02"}} 

So you can go to the file location /code.twig and edit it in a normal editor. I assume it gets overwritten by a database edit though. The error might be related to my layout which just has a {{ page.code }} tag in it, but I guess it's an object and it needs to be {{ page.code | render }} or something.

psaunders  —  7 years ago

Looks like the syntax for the code page is something like

{{ page.codefieldname.render }} or {{ page.codefieldname.rendered }} 

Which corespond to function names in the EditorFieldTypePresenter class. Seems that the .rendered() is deprecated so I'm going with .render. That gives me:

Twig_Error_Syntax: Unknown "posts" tag in "/Users/admin/www/websitename/storage/streams/default/pages/home_page_pages/3/code.twig" at line 1. in /Users/admin/www/website/vendor/twig/twig/lib/Twig/Parser.php:181

I'm wondering if the code snippet can't just be placed in a page. If I put some HTML in the box it renders the content out as a quoted string with any of {{ page.code.content }} {{ page.code.render }} which is weird. The syntax is definately twig but I've never used it before so I'm giving up on that whole approach and going to look into routes.

psaunders  —  7 years ago

So routes... It's not the routes/web.php file seeing as that still seems to be pointing at the laravel welcome page. So I'm looking around for a routes.php or web.php file first, and then I'm going to look for something that starts with

Route::get('/', 'Anomaly\PagesModule\Http\Controller\PagesController

Because something like that is currently being called to have the site in the state that it is currently in. Hopefully I can edit that location. There's a PublishRoutes console command which looks like it generates a simple get request route to a "welcome" view and puts it in the resources folder. Apparently that didn't run during my setup. I created the file manually but that didn't seem to do anything.

psaunders  —  7 years ago

Running vendor:publish CLI command did publish a few vendor files but they all seemed to be part of other Laravel packages. I found a class called IncludeRoutes which again tries to run the getResourcesPath('routes.php') so i thought I would do a dd() on that command and see where it is evaluating. It's not going to the resources directory, it's going to /resources/default/routes.php, that directory doesn't even exist. After manually creating the folder, file and putting in Ryan's code from above I have got a website with the posts module as the home page.

ryanthompson  —  7 years ago

What's the content of code in your page?

ryanthompson  —  7 years ago
entries("namespace","stream_slug")

Is the proper syntax. Your error says you've got Twig syntax issue in there so paste that and we'll be set.

You're spot on on the render btw: {{ page.field_slug.render|raw }}. {{ page.field_slug|raw }} is the same thing but it uses __toString() magic to return render() which will hide errors since __toString() is not allowed to throw exceptions in PHP. So for best results use the .render|raw one.

psaunders  —  7 years ago

Yeah I'm still a little mystified about that one. Seeing as I'm the only person editing the site I'd probably drop quite a bit of code into the GUI.

{% posts = entries('posts').paginate() %}
{% for post in posts %}
    <p>
        {{ post.title }}
    </p>
{% endfor %}
{{ posts.links|raw }}
ryanthompson  —  7 years ago

You're close! You need to use set:

{% set posts = entries('posts').paginate() %}
psaunders  —  7 years ago

OK, looks like the docs are missing it here:

https://pyrocms.com/documentation/streams-plugin/latest#usage

I'd submit a PR but I don't know where you keep these

ryanthompson  —  7 years ago

Thanks, fixed! Must have been a long night cause there were multiple of that set missing in there - no repo for docs just mention it to me like this 😊