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

Question : View component (Craft 3.4.0) #6015

Closed
ryssbowh opened this issue May 2, 2020 · 6 comments
Closed

Question : View component (Craft 3.4.0) #6015

ryssbowh opened this issue May 2, 2020 · 6 comments
Labels
enhancement improvements to existing features extensibility 🔌 features related to plugin/module dev
Milestone

Comments

@ryssbowh
Copy link

ryssbowh commented May 2, 2020

Hiya,

I'm a beginner at Craft and I'm giving it a good go to see if it would fit my work's needs.
So far it's been fantastic, thanks for all the efforts, really easy to get into it, seems to be spot on for us.

One thing I struggled with is to modify the template engine. Use case : I want to create a plugin that allows me to have different themes and switch from one to the other in the back end according to some rules (one rule could be a different site, or it could be per page, per type of browser (mobile, tablet) etc ...)
Theming would be a way for me to have inheritance between views and packaging, which at the end of the day makes development faster.

I tried the template root way and quickly realised it can't work, somehow every folder must be keyed with a subfolder, I want the view engine to look in a new folder entirely.
I want to do exactly what's done in the View class when considering different sites (method _resolveTemplateInternal), just I want that folder to be configurable, not hard coded with the site's handle.

So I tried extending the View class and replaced it in the config (I'm not sure how to do that with a plugin yet), and I made it work, but only after changing all private functions/attributes of the View class to protected so I could extend them. Which is dirty, but I just wanted to see if it could work.

So I was wondering if those private functions/attributes were private on purpose, do you not want them changed ? Or am I looking at the problem in a wrong way ?
Or instead giving us more entry points with events during the template paths resolving process ?
Or allowing whole new folders in the template root variables ?
Or any other suggestions ?

Thanks for your time

@ryssbowh ryssbowh changed the title Question : View component Question : View component (Craft 3.4.0) May 2, 2020
@brandonkelly
Copy link
Member

I know you said you looked into template roots, but I do think would be a good solution here.

You could register one single template root called _theme, let’s say, and set that to the appropriate template folder depending on site or whatever metric you care about. Then your normal templates in the templates/ folder can start including/extending/importing from any templates in _theme/.... Just make sure that each of your themes have the same structure.

@ryssbowh
Copy link
Author

ryssbowh commented May 5, 2020

Thanks,

I thought that too, and tried it :

  • It means now that all the templates I reference manually in the back end must be prefixed by _theme in order to be found, which is ok, just not ideal.
  • Also, template roots don't accept arrays as parameters, so if I want my themes to extend each other I would need several locations for each root template, so that if one template is not found in a theme, the system looks in the extended theme template folder.
    My template roots before they are used by the View component :
[
    '_theme' => [
        0 => [
            0 => '/var/www/craft/themes/Very beautiful/templates/'
            1 => '/var/www/craft/themes/Beautiful/templates/'
        ]
    ]
]

Which throws an error (View.php on line 887), the path now being an array.

And also it means I can't override any given system template with this. Could be useful to have a theme to override some of the back end/plugins templates.

Actually I think all this could be resolved if we could specificy an empty key pointing to an array for the roots templates, something like that :

$event->roots[''] = array_merge([
    0 => '/var/www/craft/themes/Very beautiful/templates/'
    1 => '/var/www/craft/themes/Beautiful/templates/'
], $event->roots[''])

Would you be open to that ? I can look into it if you think it has a chance of making it to the code base. Or would that be too big of a change ?

@brandonkelly brandonkelly reopened this May 5, 2020
@brandonkelly brandonkelly added enhancement improvements to existing features extensibility 🔌 features related to plugin/module dev labels May 5, 2020
@brandonkelly brandonkelly added this to the 3.5 milestone May 5, 2020
brandonkelly added a commit that referenced this issue May 5, 2020
@brandonkelly
Copy link
Member

Good points. I just made the following changes for 3.5:

  • Now possible to specify template roots without a template prefix ('')
  • Now possible to specify multiple directories per template root

(In other words, made your example possible.)

Craft 3.5 is currently in Beta. To update to it now, change your craftcms/cms requirement in composer.json to "3.5.x-dev", and then run composer update.

@ryssbowh
Copy link
Author

ryssbowh commented May 9, 2020

Wonderful, thanks

@piotrpog
Copy link

@brandonkelly
I have a question. Will it be possible to overwrite some template files in control panel using this functionality, from within php module?

@brandonkelly
Copy link
Member

@piotrpog No, because Craft’s own template path will be matched before it looks at custom template roots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvements to existing features extensibility 🔌 features related to plugin/module dev
Projects
None yet
Development

No branches or pull requests

3 participants