Jump to content

proposition: $page->get($field, $unformatted=false)


Adam Kiss
 Share

Recommended Posts

I just needed to get unformatted field value, and it makes ton of sense to me to enhance $page->get with optional parameter of formatted. I'm not sure which version makes more sense, but I gues that $unformatted?

<?php

 //either pre-defined formatted
 public function get($field, $formatted = true);

 //you use
 $page->get('date', false); //unformatted

 //or pre-defined unformatted
 public function get($field, $unformatted = false);

 //you use
 $page->get('date', true); //unformatted
Link to comment
Share on other sites

There is already is the getUnformatted() function:

<?php

// will output a unix timestamp
echo $page->getUnformatted('date'); 

// these will output a formatted date
echo $page->date; 
echo $page->get('date'); // alternate syntax

getUnformatted() works the same way as the get() function, only the output is never run through the outputFormatting filters. Another way you can do it:

<?php

// below will output a formatted date
echo $page->date; 

$page->setOutputFormatting(false);

// below will be a timestamp rather than a formatted date
echo $page->date; 

// anything else you get will also be unformatted
// i.e. no entity encoders, etc. 

// optional: remember to turn it back on when done:
$page->setOutputFormatting(true); 
Link to comment
Share on other sites

I understand, that makes sense.

You can probably tell I have a preference for functions with 1 or 0 arguments for public API stuff. The strategy with the API is that if functions start taking multiple arguments, then it should be split into multiple functions. But this isn't as much of an issue with the get() function, especially since the second argument is optional as you suggested. But in this case, the get() function is common to all classes in ProcessWire based on the WireData class. If I add an argument, I have to add it to all of them, but the unformatted option would only be applicable to one of them (Page). If I just add it to the Page class, PHP will complain with strict mode errors. :) So I do think it's a good idea you proposed, but there are a couple of good reasons why we probably shouldn't. Do you think that getUnformatted() is too verbose? We could always look at adding a shorter alternate, like getu() or something, whether native to the class or from a module.

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