Jump to content

OutputFormatting Workflow


Recommended Posts

I hate to keep harping on this, but managing and keeping track of the current OutputFormatting state on pages is still often a pain point for me during module development, and I'm wondering how others are handling this.

I've been trying to follow the pattern in each of my functions of calling $of = $p->of(false) at the top of the function and setting it back to its prior state at the end of the function with $p->of($of). One of the big issues I come across is that I often need to turn off formatting for more than a single page, as I am often getting values from pages inside of page fields. For example, I might try to call $p->pageField->title, and expect that I am going to get an unformatted value because I turned off output formatting on $p, but in fact I get a formatted value because title is part of an entirely different page.

I recently found out about $pages->setOutputFormatting(false), which apparently turns off formatting for ALL pages simultaneously. This function isn't listed in the cheatsheet and I couldn't find any documentation about it, so I'm not sure whether its use is recommended or not. 

I feel like the best workflow would be to turn off output formatting for all modules and "business logic" by default. I could then use getFormatted() on individual fields only when a formatted value is actually desired. For template files, on the other hand, I would like to have output formatting ON by default. I'm not sure how to best implement this, though, because PW doesn't explicitly turn on formatting for template files and generally assumes that it is on.

I'd be interested in hearing what other module developers are doing.

Link to comment
Share on other sites

Output formatting is always off in modules and the backend (by default)...and always on in the fronted (template files) (by default) just like it's stated here (your thread)...-) Maybe I'm misunderstanding you? Conversely, one would normally check it is on in formatValue() method in Fieldtypes since that is meant for output.

Edited by kongondo
Link to comment
Share on other sites

I have heard that said a few times, but it seems to be on by default everywhere for me. I wonder if there is a module I'm using that is turning it on? I just searched my project for "pages->setOutputFormatting" and "('pages')->setOutputFormatting" but only see two results: Once where it is being switched off in admin.php and once where it is switched on in ProcessPageView.module.

Link to comment
Share on other sites

I have heard that said a few times, but it seems to be on by default everywhere for me. I wonder if there is a module I'm using that is turning it on? I just searched my project for "pages->setOutputFormatting" and "('pages')->setOutputFormatting" but only see two results: Once where it is being switched off in admin.php and once where it is switched on in ProcessPageView.module.

By your project, you mean your template files and custom modules? If not, you know it could also be called using the short syntax, $page->of(). If your project is not on a production server, maybe turn off modules one by one (maybe autoload ones first) to try to debug this.

Link to comment
Share on other sites

You cant be sure if a module is used in frontend context or backend. So turning on on or off by default isn't going to work. But then I don't know what's so difficult about it.

Yeah, that's what I was thinking. If you call a method from the module in the front end, then outputformatting would be on, so there's no way to really know for sure. That is exactly what's "so difficult about it." You have to be continually cognizant of what state you're in, which leaves a lot of places in the code where errors can creep in which may only show themselves under very specific scenarios. In other words, I can have a function that will work just fine during testing in one place, but when called from somewhere else will fail due to the state of output formatting on a particular page--which was modified by some other function in the meantime. That's not good.

If I could, I would just use getUnformatted() all the time and call it a day. But even that won't solve the problem, since PW will still error if it happens that formatting is turned on for a page that I call save() on.

By your project, you mean your template files and custom modules? If not, you know it could also be called using the short syntax, $page->of(). If your project is not on a production server, maybe turn off modules one by one (maybe autoload ones first) to try to debug this.

I meant the entire code base for the website. I don't believe there is a short syntax for the $pages->setOutputFormatting(). I'll have to do some more testing and see what's causing this. Thanks for your help.

Link to comment
Share on other sites

Is it not possible to separate your methods; those exclusive for the frontend and those for the backend? Or even better, throw them into different Classes? E.g. a MarkupMyClass vs OtherModuleClassForBackend? Just thinking out loud here...

Link to comment
Share on other sites

A module is either designed for backend only. Then you know outputformatting is off always. Or for front-end, then it's always on. If you do stuff in your module that requires outputformatting off or on, just make sure and set it according to. If you're not sure, you should remain state with:

$stateBefore = $page->of();
$page->of(false);
...
$page->of($stateBefore);
Link to comment
Share on other sites

A module is either designed for backend only. Then you know outputformatting is off always. Or for front-end, then it's always on. If you do stuff in your module that requires outputformatting off or on, just make sure and set it according to. If you're not sure, you should remain state with:

$stateBefore = $page->of();
$page->of(false);
...
$page->of($stateBefore);

I guess I should clarify. In this particular instance, my "module" is built for a particular website and it contains most of the hooks and custom logic for the entire site (the "model"). It also autoloads various custom classes that I have built for the site. I am using hooks in my module to effectively extend the functionality of the Page class for different templates. My controller code for each template is in the templates folder, however. So I presume that this is the reason why output formatting appears to be on "in the back end", because most of the time my hooks and functions are ultimately being called from a template file, which is loaded after PW has already switched on formatting. Does that sound right?

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...