2

I have created custom template for PageList block and need it to be used to get required visual effect. User needs to have possibility to add new instances of the block to page (they are used as slides for carousel plugin) but in that case he needs to remember to change block template from default to the custom one each time when new block instance (new slide) is added.

Is there any way to force concrete5 to automatically use custom block template instead default one?

2 Answers2

5

Yes, you can enforce this on a per-area basis. In your theme's page type template, find the code for the editable area you want to enforce this custom template on, and call the setCustomTemplate function. For example:

$a = new Area('Main');
$a->setCustomTemplate('page_list', 'templates/your_custom_template');
$a->display($c);

The first argument is the blocktype handle for the block you want to apply this to, and the second argument is the custom template (note that there is not a .php extension at the end of that -- the C5 system will add that automatically).

Jordan Lev
  • 2,773
  • 2
  • 26
  • 39
  • and when I use 2nd argument as `templates/your_custom_template` this is resolved to `ROOT/blocks/block_name/templates/your_custom_template` - do I understand it correctly? – Walking In The Darkness Apr 01 '13 at 01:00
  • The 2nd argument is resolved using the usual C5 cascading override rules... first it looks in `ROOT/blocks/block_name/`, if it doesn't find anything there then it looks in `ROOT/packages/*/blocks/block_name/`, if it doesn't find anything in any packages, then it looks in `ROOT/concrete/blocks/block_name/`. Inside those directories, it will look for `/templates/your_custom_template.php`, because we passed in `templates/your_custom_template`. Note that in this situation, there is nothing special about the `templates` directory... that is only involved because we put `templates/` in the 2nd arg. – Jordan Lev Apr 01 '13 at 16:11
  • I had to add .php extension to the setCustomTemplate in order to make it work, for example: $a->setCustomTemplate('page_list', 'templates/your_custom_template.php'); – Rami Sep 26 '13 at 11:27
1

It's possible, I did it for work a while ago. Hopefully the link will below will help you get started. There is a section called Override and Modify near the middle, although you are probably going to have to scim through other sections to know what they are referring to.

http://www.concrete5.org/documentation/recorded-trainings/building-blocks/basic-block-development-four/

This may also be helpful:

http://www.concrete5.org/documentation/how-tos/designers/change-how-a-block-looks-templates/

Andrew
  • 2,013
  • 1
  • 23
  • 38
  • `/blocks/block_name/view.php` instead of `/blocks/block_name/templates/template_name/view.php` would solve this? – Walking In The Darkness Mar 30 '13 at 00:47
  • If yes, that's definitely thing worth to remember for future however in this case it wouldn't be desired solution - this is about PageList block that is used in more than one place and I have two custom templates used in different places of website (different template used for carousel plugin and different for typical listing of latest articles within section). I want to be able to use both of them and overriding would be almost the same as editing the original `view.php` file (with no risk of being overwritten when CMS is being updated) – Walking In The Darkness Mar 30 '13 at 00:48
  • That is probably what Andrew meant, and that's the solution I would have given you. You can always copy the default one to `/blocks/block_name/templates/original/view.php` or something so that's still an option. But if you're looking to get the modified block to be used "automatically" in some situations (e.g. pages), but not others, then this wouldn't be the best solution. – James S Mar 31 '13 at 01:09