htmlliquidlocomotivecms

Access single content entry in LocomotiveCMS


I want to show a single content entry (from one content type) on a page and I want to access this page directly with a link.

So far I've created the content type "posts" (with wagon generate ...). It contains the fields "title","date" and "body". On the page "posts", all title's of the posts are listed and when you click on one of the title, you should be redirected to the subpage "post" which contains the rest of the content (depending on which post you selected).

posts.liquid:

{% extends parent %}
     {% block main %}

            {% for post in contents.posts%}
                   <a href="/{{ post._slug }}"><li>{{ post.date }} - {{ post.titel }}  </li></a>
            {% endfor %}

    {% endblock %}

This lists all posts.

post.liquid:

{% extends parent %}
     {% block main %}

       <h2>{{post.title}}</h2>
       <h3>{{post.date}}</h3>
       <p>{{post.body}}</p>

      {% endblock %}

And this shoul be the template for the rest of the content on a single page.

How can I link the list elemnts to the correct post? I'm using wagon to develop the site local.


Solution

  • I found a solution to my problem.

    To access only one specific entry in the content type posts, you need to create a template which holds the content with the correct layout.

    This means you need a file named "content-type-template.liquid" and this file has to be placed in a folder (in my case named "post") to define the parent:

    /posts.liquid                          # Holds a list of all Posts
    /post/content-type-template.liquid     # Holds the layout for only one post
    

    Also in the top of content-type-template.liquid you need to define the content-type and the slug:

    --- 
    title: Post Template
    content_type: post
    slug: content_type_template
    ---
    

    The fields of the content type are now reachable with the following syntax:

    {% extends parent %}
     {% block main %}
    
       <h2>{{post.title}}</h2>
       <h3>{{post.date}}</h3>
       <p>{{post.body}}</p>
    
      {% endblock %}
    

    If the content type is for example named "product", you need to rename everything above called "post" with "product".

    Finally you can reach a single entry with it's slug.

    {% extends parent %}
     {% block main %}
    
            {% for post in contents.posts%}
                   <a href="/post/{{ post._slug }}"><li>{{ post.date }} - {{ post.titel }}  </li></a>
            {% endfor %}
    
    {% endblock %}
    

    Here are some links that helped me:

    http://www.tommyblue.it/2011/02/28/how-to-build-a-website-with-locomotive-cms-from-scratch/

    http://doc.locomotivecms.com/references/api/objects/content-entry