Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create TimberView object to handle view rendering #261

Closed
8 tasks
jarednova opened this issue Jun 16, 2014 · 7 comments
Closed
8 tasks

Create TimberView object to handle view rendering #261

jarednova opened this issue Jun 16, 2014 · 7 comments

Comments

@jarednova
Copy link
Member

This is from the discussion in #248 and the #259 epic. Create a new object to better organize how templates are rendered and locations are chosen. This will deprecate (but not eliminate) the current static-based approach to data and rendering.

Acceptance

  • Existing functionality for Timber::render and Timber::compile still work
  • Themes and multiple plugins can set different rules for the location of twig files
  • New methods are documented in wiki
  • Method for populating context for template is documented
  • Functionality for caching is preserved, new syntax/method is documented
  • Review API with 2 collaborators for sanity-check
  • Reference of files within twig (extension, inclusion) is considered
  • Reference of files within twig (extension, inclusion) is documented

... this work is currently in: https://github.com/jarednova/timber/tree/timber-view. Here's a sample of the API that someone might use in single.php:

Basic theme example with standard locations

$view = new TimberView();
$view->context['post'] = new TimberPost();
$view->cache(600);
$view->render('single.twig');

Plugin example with custom locations

$view = new TimberView('my-plugin-context');
$view->cache(600);
$view->context['days'] = my_plugin_get_days_or_something();
$templates = array(
    get_template_directory(),
    get_template_directory().'/templates',
    __DIR__.'/templates'
);
return $view->compile('calendar-overview.twig', $templates);
@jarednova jarednova self-assigned this Jun 16, 2014
@jarednova jarednova mentioned this issue Jun 16, 2014
18 tasks
@bryanaka
Copy link

I really like the idea behind this API. How do you feel about TIMBER_TEMPLATES_PATH constant holding the possible template locations? I feel this is more similar to configuration than anything. The only problem is that a array constant would have to be serialized and unserialized 😒

@jarednova
Copy link
Member Author

@bryanaka, yeah, I think it's wise to have a constant like that. In my thinking, the use-case is for when you want to easily prepend or append a custom path before/after the default locations. Did you have another use-case in mind?

@bryanaka
Copy link

No, that is exactly the use-case in mind. sounds fair enough to me.

So I am basically mimicking this API in a project I just started (thus my re-entry to Timber 😉 ), the only thing I changed was exposed a set function which would then set variables on the context assoc. array, which was not a public variable. So like $view->set('post', new TimberPost());. I like it as a personal preference, but not sure if that is just Ember speaking or if this idea would feel natural to PHP devs.

@roopemerikukka
Copy link

Hi,

Is there any plans to complete this feature? I'm trying to use Timber in a plugin and would like it to work with both themes that use Timber and themes that don't. Is there any current solutions how I can bypass the fact that I cannot change the locations after first initialisation? Would it be safe to use absolute uris in the plugin and not to set the locations at all so the theme can do that if wanted?

@palmiak
Copy link
Collaborator

palmiak commented Oct 10, 2018

@jarednova - are there any plans about this?

@palmiak
Copy link
Collaborator

palmiak commented Feb 27, 2019

I wanted to use Timber in a plugin and investigated how all this works. What surprised me is the fact that we almost have the working functionalaty to use separate templates even for each plugin / theme.

There is something like $paths = apply_filters('timber/loader/paths', $paths); with this we can do something like this:

function filter_paths() {
    return [__DIR__.'/front'];
}

add_filter('timber/loader/paths', 'filter_paths');
Timber::render('admin.twig', $context);
remove_filter('timber/loader/paths', 'filter_paths');

I know it's not pretty, but it's a quite well functioning workaround to problem with $locations which was mentioned for years.

I think it solves all the problems mentioned in #248. Or maybe it's too late and I don't see some obvious problem :)

@jarednova
Copy link
Member Author

I'm closing all 2+ year old issues. If someone wants to pick this up, let's make it a fresh issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants