Block Common Code: Where to Put?
Permalink
Let's say I'm creating a block which might appear multiple times on a given page. And let's also say that there are some utility functions or classes upon which this block type relies. In a non-C5 environment, I'd just include the common code once near the beginning so that each block instance would have access to the methods/functions. However, it's not clear how best to do this in C5. Would I simply call require_once from view.php? That seems like unnecessary overhead since it'd be called for each instance on the page but really only needs to be called once. Is there a way to include/require a block-specific file at the page level somehow?
Thanks for any insight you can provide,
-Steve
Thanks for any insight you can provide,
-Steve
Thanks, but that doesn't do what I need. It includes the file for each block instance on the page. The regular old require_once function does include the file just once (regardless of the number of block instances on the page), but the contents of the included file are available only to the first instance. Let me rephrase my question...
Is there a way to include a file from within a block only ONCE and have that file's contents be available to all block instances on the page?
-Steve
Is there a way to include a file from within a block only ONCE and have that file's contents be available to all block instances on the page?
-Steve
I have a feeling I'm not describing the problem very well, so I'll try to illustrate with a concrete example (pun intended). I downloaded the "basic_test" block to experiment and learn the ropes. As most of you probably know, all it does is output some user-defined content. The content can be different for each block and is stored in the database. So let's say you have a string munger class that's designed specifically to process the "basic_test" block content. I want to run each block's content through the munger before it's displayed. My question is, where do I put that munger class code? Obviously, the class definition code doesn't need to be loaded for each block, but each block does need to instantiate the class in order to output the munged string. IOW, where do I put the code that's "shared" among all block instances, and how do I "include" it?
Can anyone help? I must be overlooking something obvious.
-Steve
Can anyone help? I must be overlooking something obvious.
-Steve
Ok, the "controller.php" file seems to be the place for block-specific code that's shared among instances. Or is there a more appropriate location?
-Steve
-Steve
that's it really, unless you abstract to a model, but really you want to put something into cache using a sort of key
learn the ropes first, i'll be around to give input through the forums.
I know it sucks but there are some pretty neat apps out there that can provide a sort of guidelines for this sort of thing, although I will be going through my blog app and adding some optimizations when i get back.
learn the ropes first, i'll be around to give input through the forums.
I know it sucks but there are some pretty neat apps out there that can provide a sort of guidelines for this sort of thing, although I will be going through my blog app and adding some optimizations when i get back.
http://docs.concrete5addons.com/...
and take a look at the View::inc() method which is reimplemented in BlockView::inc()