How can I make additional variables available in my views (where is the controller)?

to-doc
Tags: #<Tag:0x00007f81c72139f0>

(Danny) #1

How can I make additional variables available in my views (where is the controller)?


Unable to assign Article to blog when Blog (Parent) is created after Article
(Aaron Russell) #2

Good question. The front end routes are handled by the front end controller, but right now it can’t it can’t be easily overridden (this may change in future) and so you don’t need to worry about it.

Instead, PushType provides a number of filters that can be defined in the ApplicationController:

class ApplicationController < ActionController::Base

  before_node_load do
    # This block will be called before the @node instance is loaded
  end

  before_node_action do
    # This block will be called after the @node instance is loaded, but before the view is rendered
  end

  before_node_action do
    # This block will be executed after the view is rendered
  end

  # You can pass :only and/or :except options to any of the methods above
  # to determine which node types the filters get triggered on
  before_node_action only: [:page, :article] do
    @popular_posts = Article.published.popular.limit(10)
  end

  # If preferred, you can define your logic in a method instead of passing a block
  # and chain methods in one filter
  before_node_action :load_hero_banner, :load_popular_posts, only: :home_page

  private

  def load_hero_banner
    @hero_banner = HeroBanner.published.first
  end

  def load_popular_posts
    @popular_posts = Article.published.popular.limit(10)
  end

end

Hope this helps :slight_smile:


(Danny) #3

Thanks! Thats exactly what I’m looking for.
Is there a reason why the documentation is incomplete at the moment? Your answers are basically documentation and all you’d have to do is copy paste them over :stuck_out_tongue:

What if you had to pass in parameters?
For example, each Article has a tag_list and I want to display those tags on the Article view page with a link to a sort of tag-list page which lists all Articles with that tag.
How would I go about that?
I created a TagList node which I can use for it. How do I tell it which tag I want to display? The node Tag does not exist. We are talking about tag_list. I like the tag_list view but have a feeling this use case requires Tag to be a node and associated with an Article.


(Aaron Russell) #4

Just time constraints. At the moment my focus is tilted towards on developing the product and helping people through the forum. But you’re right a lot of the answers here will be able to be reused in the docs. The docs will become a bigger priority soon.

Another good question. In short, if you’re using tag_list you’ll have to create your own controller in Rails and pass the tag as a parameter and manually query the articles using the methods mentioned above.

This is a topic I want to write more about though - generally how to approach “taxonomies” in PushType as there are a few different ways of thinking about this. I’ll try and post something up over the next few days and comment you in for your attention.


(Danny) #5

That would be amazing. This is a great CMS and it’s only limiting factor at the moment is the lack of documentation and features (as you’re just starting out).

Once I’m more proficient, I would be happy to contribute as well. But we’ll cross that bridge when we get to it.