How do I choose part of a template at runtime? (v 0.8)

47 views
Skip to first unread message

Chris Hansen

unread,
Dec 12, 2013, 11:25:51 AM12/12/13
to pak...@googlegroups.com
Good morning!

What is the proper way to set the contents of a div dynamically?  Let me clarify what I am trying to do:  basically, I have a couple of menus that populate dynamically, and based on a user's selections, the content of the page will change.  It could be static, or it could be built from queried data (it depends on the selection.)  Ideally, there would be view files to contain these sub-sections. Now I know in 0.7 that sections could be populated using div IDs and appropriately named files; but I want to set the included file dynamically.

Any help would be appreciated.  Thanks!

--Chris

Bryan Powell

unread,
Dec 12, 2013, 7:56:03 PM12/12/13
to pak...@googlegroups.com
You actually have quite a bit of flexibility without introducing backend code. Here's an example based on the following view hierarchy:

views/
  _nav.html
  index.html (includes "nav")
  sub1/
    _nav.html
  sub2/
    _nav.html

Here we have three versions of the nav. Setting the view path to "/" would result in a view compiled with "index.html" and "/_nav.html". If the view path is "/sub1", the resulting view would be compiled with "index.html" and "/sub1/_nav.html". And so on.

Letting the views build themselves and only binding data from the backend code is always the recommended approach. Does this approach solve your particular problem?

Bryan P.


--
You received this message because you are subscribed to the Google Groups "Pakyow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pakyow+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Chris Hansen

unread,
Dec 12, 2013, 11:43:59 PM12/12/13
to pak...@googlegroups.com
I don't think that solves my problem.  I do want to let the views build themselves, but the components of the view may change based on user preferences and/or whether the user is logged in (for example.)  So, based on user preferences, a layout would load with certain components, and then another bit of information might be pulled via Ajax (I remember in the docs that it specifies how to load parts) to replace one of the components based on a user interaction.  Basically, there is header matter which will always be the same, and then a left column and a right column -- each of which will contain content which is independent of the other column.  I want to say, in essence "okay, for this time around, your template parts live here and there", and then just the view build itself based on that information.  Does that make sense?  Can I set the view path for different parts of a template separately?  Can I have different view paths for different parts of the page?  Maybe I should be building this a different way...

Thanks for all your help.

--Chris

Bryan Powell

unread,
Dec 13, 2013, 9:57:48 PM12/13/13
to pak...@googlegroups.com
Doing this from the backend is possible using an approach like this:

presenter.page = Page.load('app/views/partials.html').include_partials(
  a_partial: Partial.load('app/views/_partials/_a_partial.html')
)

Here, "partials.html" includes a partial named "a_partial". The `include_partials` method accepts a map where the key is the name of the partial and the value is a Partial instance. Since there's no way to do this through a ViewStore, you have to provide the full path to the page/partial files. Setting the page on presenter will trigger a rebuild of the view.

It's a bit verbose, but it works.

Bryan P.

bryanp

unread,
Dec 15, 2013, 10:58:14 PM12/15/13
to pak...@googlegroups.com
I spent a bit of time this weekend thinking about how to improve backend view composition. Thought it might be of interest to you. Rather than the example I posted Friday, you could do this:

presenter.compose do
  at('partials').includes(a_partial: '_partials/a_partial')
end

ViewComposer is currently available in master. You can see the full spec here: https://github.com/metabahn/pakyow/blob/master/pakyow-presenter/test/view_composer_test.rb

Any thoughts you have would be appreciated!

Bryan P.

bryanp

unread,
Dec 16, 2013, 11:11:39 PM12/16/13
to pak...@googlegroups.com
Revisited this and made some tweaks to the syntax. Passing a block is still supported (as is method chaining) but you can also pass as arguments:

presenter.compose_at('partials', includes: { a_partial: '_partials/a_partial' })

See the updated specs for more.

Bryan P.

On Thursday, December 12, 2013 10:25:51 AM UTC-6, Chris Hansen wrote:

Chris Hansen

unread,
Dec 17, 2013, 10:47:21 AM12/17/13
to pak...@googlegroups.com
Wow, thanks Bryan!  I'll work with this today and let you know if I run into any problems, etc.

Best,

--Chris

Chris Hansen

unread,
Dec 17, 2013, 4:48:47 PM12/17/13
to pak...@googlegroups.com
I've been looking at this today.  It's beautiful!  As I've been thinking things over, I may use a hybrid approach on some of the partials (e.g. put a canvas in place, and populate the data via JSON from an AJAX request) -- I'm working with some rather complex, somewhat heterogenous data.  At any rate, thanks again!

--Chris

On Dec 16, 2013, at 9:11 PM, bryanp <br...@metabahn.com> wrote:

Reply all
Reply to author
Forward
0 new messages