reuse .php in multiple places

Permalink
When you simply want to reuse a block of php code via a require statement where is the best place to keep this php file in Concrete5.

I thought "tools" would be the way to go but then saw that they are being deprecated. Routing or packages seems way more complicated than I need.

I'm attempting to port a previous website to concrete5 and previously I simple used a php require command.

I attempted to create a new block type which works well when added to page templates. However, when trying to pull the block into a single page programmatically I can't figure out the method to do this.

Any suggestions or examples would be appreciated.
Thanks.

 
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
bonovasitch replied on at Permalink Reply
Thank you for pointing me in the right direction. I hadn't come across elements yet.

I'm still in evaluation mode so starting with a standard free theme. It seems that elements have to be theme based. Is this so? I would rather place the file in a global elements location that could be picked up by any theme I end up using.

I noticed the \application\elements but received a file not found error when moving the file here. The \concrete\elements has tonnes of stuff in it. Do you mind explaining the difference with these elements directories?
JohntheFish replied on at Permalink Reply
JohntheFish
There are 2 kinds of elements that are not really the same thing.

1) elements in /application/elements or in /packages/package_handle/elements/.
Don't use /concrete/- that is reserved for the core.

A typical insert uses a path relative to the relevant /elements/ dir
View::element('element_name', [parameter_list]);
// or
View::element('element_name', [parameter_list], 'package_handle');


2) You can also have elements in theme or block views, where the path is relative to the view inserting the element. A typical insert is
$this->inc('element_name');


You can find plenty of examples by searching for such calls in the core or in free packages.
bonovasitch replied on at Permalink Reply
That's great info!

Thanks for clearing that up.
JohntheFish replied on at Permalink Reply
JohntheFish
For your single page, the easiest way is to modify the template (theme view.php) to have area(s) in the place you want to add blocks. Then you add/edit blocks as usual. The advantage is that such blocks can be cached.

Blocks coded into a page are not cached (unless you use the new Advanced Cache addon)