Jump to content

Outputting javascript or css from a field is bad practise ?


pwired
 Share

Recommended Posts

Processwire outputs its fields exactly how it was entered in the fields regardles the used text formatting,
so imho you could enter any javascript or css as text in a field and output it where you need it in a webpage. Or even use an unpublished hidden page with fields holding js and css. I am working on a website where the top navigation menu is changed with different style and buttons, depending on the day or week. The js and css for the different styles and buttons I want to pull from fields. Would this be bad practise using fields for this ?

Link to comment
Share on other sites

I am not sure of the good or bad, but generally I find the idea of passing any code via a field makes me uncomfortable.

However, you can do other things.

For instance, if you have various styles in a CSS file, you can add those styles via a field that just adds the class name to the element.

Or, you can include entire style sheets and JS scripts using a field for the page:

<script src="<?php echo $page->myscriptfield; ?>" ......

The nice thing about working that way is that you can create a series of style sheets and JS scripts and then the client can choose them using a page field as a select. You then don't have the worry of someone entering something in the field that breaks the entire page, and you can fine tune your "themes" without having to edit pages.

If they go together in pairs, you can create a template for your page field selects that has three fields:

title - nice description

js - text field with the name of the script

css - text field with the name of the style sheet

Using a page field, you select using the nice description, then use the other two fields to call up the JS and CSS.

Lots of variations possible without entering any code into a field.

  • Like 1
Link to comment
Share on other sites

you can add those styles via a field that just adds the class name to the element.

Or, you can include entire style sheets and JS scripts using a field for the page

Using a page field, you select using the nice description, then use the other two fields to call up the JS and CSS.

Thanks Joss, you gave me some good ideas there to work with. I prefer to keep css and js inside the db instead of external files so I can edit them inside PW.

While I was surfing the forum with google I found this also:

http://processwire.com/talk/topic/2782-template-design-better-route/

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...