Applying code snippets automatically

11 posts by 4 authors in: Forums > CMS Builder
Last Post: February 18, 2010   (RSS)

Hi All,


Sorry for the title, but I couldn't think of a better way to describe what I'm trying to explore.

I've been using a number of code snippets to insure that certain situations don't cause issues. At present, I need to insert the correct code snippet each time I want to invoke a particular check.

What I'd like to do is create a plugin, a class, or maybe a few PHP files, one for list viewers and another for single and detail viewers, that I can "include" in all of my pages that will apply all of these functions to that page.

I don't even know if any of this this is realistic or if anyone has tried it before. I'm sure that there are complications, especially where detail pages are concerned, but here are a few examples of some of the things I had in mind.

1) Always checking for http:// in a URL (I can make sure that the characters "url" appear in each field name where this check would be required but I don't know how to check for the occurance of url, _url, or _url_ in a field name.)

<?PHP if (!preg_match("/^http:\/\//i", $record['my_url_field'])) {
$record['my_url_field'] = "http://" . $record['my_url_field']; } ?>


2) Always checking for errant spaces left in an otherwise blank field that would defeat an "if" test.

<?php if ($record['my_field'] && !ctype_space($record['my_field'])): ?>

3) Replacing a placeholder "-" with a space.

<?PHP $record['my_field'] = preg_replace("/[-]/", "&nbsp;", $record['my_field'] ); ?>

4) Capitalizing field contents on demand

<?php $title =($record['my_field']); ?>
<?php echo strtoupper($my_field); ?>


I'm sure that other users have their favorites as well.

I thought that I'd throw the concept out on the forum and get some feedback.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Applying code snippets automatically

By Chris - February 10, 2010

Hi Jerry,

You can package up these snippets into functions, but there's no simple way to get things to happen automatically.

For example:

function ensure_url($url) {
if (!preg_match("/^http:\/\//i", $url)) {
$url = "http://" . $url;
}
return $url;
}


<?php echo ensure_url($record['my_url_field']) ?>

In this way, you can build up a library of your own helper functions. You could even toss them into a plugin to make them always accessible. Just be careful to use unique function names.
All the best,
Chris

Re: [gkornbluth] Applying code snippets automatically

By Dave - February 11, 2010

Jerry,

Do you want those code snippets to be applied to _every_ field from every record and section, or just certain fields?

Maybe we could add a plugin filter for this.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Applying code snippets automatically

Hi Dave,

Thanks for the reply.

I think that this could easily end up a complicated mixed bag of tricks, but maybe for universality and simplicity, it should be limited to things that only fit “always applied to every field” situations.

These are the only examples that I've thought of so far, but I'm sure that there are others that would be as useful.

1 and 2 are my most often used. They should be used in every case, but sometimes I forget or get lazy.

3 and 4 are only used in special situations


1) Always check for url, _url_, or url_ in the field name

Let’s say the characters “url” are only used in names of fields that need to be checked for http:// then it could check all of those, every time.

2) errant spaces in otherwise blank fields.

I think that this also is an always. There’s never (that I can think of ) a situation where I’d want a blank space in an otherwise empty field to be a valid situation.

3) Replacing placeholders with spaces. (I use this for instances like Lightbox where a space would break the code)

Maybe there needs to be a more obscure placeholder than a “dash” and these placeholders always get replaced with spaces when echoed.

4) Capitalizing (or decapitalizing) the contents of a field

This would be an on demand thing and maybe shouldn’t be considered for this plugin.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Applying code snippets automatically

By Dave - February 11, 2010

Hi Jerry,

Thanks, I've made a note of this and put it on the list for the next version release (after the beta). We'll do some sort of plugin hook or filter.
Dave Edis - Senior Developer
interactivetools.com

Re: [equinox69] Applying code snippets automatically

By Dave - February 11, 2010

Hi equinox69,

a) There's a certain amount of if/else statements that you need to write for each different site, but can you provide examples of the common ones you are writing? We'll see what we can add to the code generator for future versions.

b) What if we made this a language string? Then you could easily modify it by creating a custom language file in /lib/languages/. And non-english speakers could modify it as well.

c) That one is on our list to move over to be a CMSB plugin so it just works on every CMSB page.

Thanks for your feedback!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Applying code snippets automatically

By Codee - February 11, 2010

a) There's a certain amount of if/else statements that you need to write for each different site, but can you provide examples of the common ones you are writing? We'll see what we can add to the code generator for future versions.

Dave, I should have been more clear on this, I'm sorry. I'm referring to if/else statements if something is in the record or not. An example that I use on nearly every piece of code for inventory items on many sites:

<?php if ($sectionnameRecord['']): ?>
<?php echo $sectionnameRecord[''] ?><br/><br/>
<?php endif ?>

b) What if we made this a language string? Then you could easily modify it by creating a custom language file in /lib/languages/. And non-english speakers could modify it as well.

That would be one way to do it. Maybe a section in CMSB documentation about modifying some of the default items like this could be helpful. Another method, possibly, is under the Admin Section, maybe having a subsection for "Website notifications" or messages that modify those messages like "No records found!" etc. This way it would be easy to make changes from the admin interface instead of hard-coding each site (less chance of danger and changing the wrong lines of code - idiot proofing).

c) That one is on our list to move over to be a CMSB plugin so it just works on every CMSB page.


Fantastic! It's a simple but GREAT and helpful piece of code. My clients appreciate the added security of it. Thanks.

Re: [equinox69] Applying code snippets automatically

Hi Guys,

I certainly second the "if" statements idea since I just got through hand coding (and debugging) about 150 fields just like those.

The messages are a good idea too. We've all got our pets.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Applying code snippets automatically

By Dave - February 15, 2010

Hi Jerry,

And what do you use the if() statements for? Adding breaks after each value? Or only showing the label and value if the value is defined?

Can you post some short example code?

Also, what editor do you use? Is it possible to search and replace to add the if statements? To automate it we need a really clear of when it should be applied and not and what the condition to test is.

Maybe something we could explore is adding hooks to allow plugins to modify how the code generator works. Because I know a few people would like to have it work in slightly different ways from how it is now.
Dave Edis - Senior Developer
interactivetools.com