alexandre19323
alexandre19323

Hello,

I'm testing October CMS (really loving it, congratulation to the dev team for this product). I precise that I'm new with October, Laravel and Twig, so I have a lot of things to learn.

I'm testing the blog plugin and I try to have a fine control over the blog post display. I can see in the doc that the component expose a "post" variable to the page where it is used.

So I tried to make {{ dump(post) }} to see what are the available variable, and I get the table in the screenshot.

https://postimg.org/image/muuzgbn1n/

The problem is that I know that there are some variables that are not displayed in that table, for example, "post.title", "post.content_html"... things I can find in plugin/rainlab/blog/components/post/default.html

If I do post.title in my cms page, it display the post title, so it works, but I don't understand why I don't see that variable in my dump.

Could you please explain me a good way to display object and available variables ? (an equivalent of dsm() or dpm() for drupal users (which is the cms I come from))

Thanks

Alex

pxpx
pxpx

Hi Alex,

Easiest thing to do is to install this plugin: http://octobercms.com/plugin/bedard-debugbar

Then you can use {{ debug(variable) }}

It should only be available when you have the site in the dev environment.

Hope this helps.

alexandre19323
alexandre19323

Hello,

Thank you for your solution, I will test that.

However, I would like to understand why I couldn't see all the variables using my {{ dump(post) }}. I'm trying to learn october, laravel etc... and I like to understand things ^^

Thanks

code200.miha
code200.miha

I had the same problems you are having I wasn't able to configure native dump function neither did any of existing plugins work as expected. I ended up making my own developer plugin that allows me to use native php_dump by calling {{ php_var_dump(twig_object) }}.

Its very simple and you can check it here https://github.com/webeks/code200-developer

pratyushpundir6424
pratyushpundir6424

@alexandre19323 The RainLab.Blog plugin is passing thru an instance of the "Model" Post (located here plugin/rainlab/blog/models/Post.php). If you look at the component file that sets up this passing thru of the required Post instance to the post template, you will notice that it's passing thru an instance of the model class itself. I am referring to the code below (located in plugin/rainlab/blog/components/Post.php):

public function onRun()
{
    $this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage');
    $this->post = $this->page['post'] = $this->loadPost();
}

protected function loadPost()
{
    $slug = $this->property('slug');

    $post = new BlogPost;

    $post = $post->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
        ? $post->transWhere('slug', $slug)
        : $post->where('slug', $slug);

    $post = $post->isPublished()->first();

    /*
     * Add a "url" helper attribute for linking to each category
     */
    if ($post && $post->categories->count()) {
        $post->categories->each(function($category){
            $category->setUrl($this->categoryPage, $this->controller);
        });
    }

    return $post;
}

Hence, what you are seeing when you do {{ dump(post) }} is basically an extended Eloquent model as seen in Laravel.

You could add this to the return statement (in the very last line above) to pass it to the template as a simple array.

return $post->toArray();

Then when you do {{ dump(post) }} in your live template, you should see what you are looking for. Also, you could leave the plugin itself alone and just add toArray call in your dump statement like so {{ dump(post.toArray) }}.

PS: Editing third party plugins directly is never a good idea. You might want to extend the RainLab.Blog plugin in case you wish to modify how it works.

pratyushpundir6424
pratyushpundir6424

@alexandre19323 The RainLab.Blog plugin is passing thru an instance of the "Model" Post (located here plugin/rainlab/blog/models/Post.php). If you look at the component file that sets up this passing thru of the required Post instance to the post template, you will notice that it's passing thru an instance of the model class itself. I am referring to the code below (located in plugin/rainlab/blog/components/Post.php):

public function onRun()
{
    $this->categoryPage = $this->page['categoryPage'] = $this->property('categoryPage');
    $this->post = $this->page['post'] = $this->loadPost();
}

protected function loadPost()
{
    $slug = $this->property('slug');

    $post = new BlogPost;

    $post = $post->isClassExtendedWith('RainLab.Translate.Behaviors.TranslatableModel')
        ? $post->transWhere('slug', $slug)
        : $post->where('slug', $slug);

    $post = $post->isPublished()->first();

    /*
     * Add a "url" helper attribute for linking to each category
     */
    if ($post && $post->categories->count()) {
        $post->categories->each(function($category){
            $category->setUrl($this->categoryPage, $this->controller);
        });
    }

    return $post;
}

Hence, what you are seeing when you do {{ dump(post) }} is basically an extended Eloquent model as seen in Laravel.

You could add this to the return statement (in the very last line above) to pass it to the template as a simple array.

return $post->toArray();

Then when you do {{ dump(post) }} in your live template, you should see what you are looking for. Also, you could leave the plugin itself alone and just add toArray call in your dump statement like so {{ dump(post.toArray) }}.

PS: Editing third party plugins directly is never a good idea. You might want to extend the RainLab.Blog plugin in case you wish to modify how it works.

1-6 of 6