On Sat, May 23, 2009 at 2:44 AM, Benedikt R. <[email protected]> wrote:
>
> Hey!
>
> Is there a cool way to parse content before doing an output?
The cool kids are using beforeRender() for this sort of thing ;-)
> In the text items in my database I got placeholders like '[BLA...]' -
> these should be replace by specific source code, that's read from some
> files.
>
> The tool should also be available in the views, and be used like this:
> $this->Articles[Article][content]->replacePlaceholders();
Why from the view, anyway? Shouldn't the content have already been
replaced? Or, do you want to update some content as it's being edited?
I guess that would make sense. I think I'd make the user submit it,
first, and redisplay it. I suppose it depends on your situation.
I would also consider storing the replacement data in a table, rather
than opening and reading a file. That way, you can grab the
replacement at the same time as your Article data. In fact, this would
allow you to do the replacement in Article::afterFind().
> I just created a component, that helps me with that:
>
> <?php
> class ElementReplacerComponent extends Object {
>
> public $replacementsFolder = 'contentReplacements/';
>
> public $searchTemplate;
> public $searchContent = '';
>
> public $adSenseTemplate;
> public $adSenseContent = '';
>
> function initialize () {
> $searchTemplate = $this->replacementsFolder .
> 'searchField.tpl';
> $adSenseTemplate = $this->replacementsFolder .
> 'googleAdSense.tpl';
>
> if ( file_exists( $searchTemplate ) ) {
> $fileHandler = fopen($searchTemplate, 'r');
> $this->searchContent = fread($fileHandler,
> filesize
> ($searchTemplate));
> fclose( $fileHandler );
> }
>
> if ( file_exists( $adSenseTemplate ) ) {
> $fileHandler = fopen($adSenseTemplate, 'r');
> $this->adSenseContent = fread($fileHandler,
> filesize
> ($adSenseTemplate));
> fclose( $fileHandler );
> }
> }
>
> function parseContentItem( &$content ) {
> $content = str_replace('[BLZ-Suche]',
> $this->searchContent,
> $content);
> $content = str_replace('[Google AdSense]',
> $this->adSenseContent,
> $content);
This looks prone to failure. Change it to [BLZ-SUCHE] &
[GOOGLE-ADSENSE] or use a case-insensitive replace because someone's
eventually going to get the case wrong.
> return 0;
> }
> }
> ?>
>
>
> Is there a better way to do that (I think there is :-D)?
>
> Best regards,
> Benedikt
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---