This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

d0pey
d0pey

Hi,

I have created now a plugin where I can manage my contents in the backend. But now I want to display them on the fronted. But I cant find how I can do this. I think I have read I can do that to write some code in the components file of the plugin but i cant find any documentation where i can understand what i have to do.

For now i have try to access my content whit a query in the template file. I do a basic query ti 6 (id 6 exisist). I copy the query form the documentation and like this:

$results = Db::select('select * from my_table_name where id = ?', [6]);
print_r($results); 

The error i get is:

syntax error, unexpected '$results' (T_VARIABLE), expecting function (T_FUNCTION) /home/ww/themes/testheme/pages/post.htm line 30

I don't understand why i get this error.....

What i tought before i create the plugin was that there is a option to make it so that i can use in my template file something like {{ books.title }}. Is there somewhere a example or howto to create this ?

My question is simple i geuss. How can i access my contents form the db on the fronted, and use them in {{ books.title }} ?

Last updated

Blackpig Creative
Blackpig Creative

You need to create a component for the plug-in and then add that component to your page(s).

In the 'onRun' method of the component you can query your table and the assign the results to a variable. Then in the components 'view' markup you can reference the variable. Something like this:

Your component file

public function onRun(){

$results = Db::select('select * from my_table_name where id = ?', [6]);
$this->books = $results;
}

and then in the default.htm of the component

{% set books = __SELF__.books %}
  class="testimonials">
<ul>
    {% for book in books %}
    <li>
        {{book.title}}
    </li>
    {% endfor %}
</ul>

Check out these 2 resources https://vimeo.com/104896353 and http://www.sitepoint.com/building-octobercms-plugins-google-analytics/

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.