Hard code attributes into template [Solved]

Permalink
I am trying hard code attributes in to a template but I cannot get it to render.

$attr = BlockType::getByHandle('page_attribute_display');
$attr->controller->attributeHandle = 'attribute_handle';
$attr->controller->displayTag = 'div';
$attr->controller->render();


NOT POSSIBLE..... YET?

 
Gondwana replied on at Permalink Reply
Gondwana
Instead of
$attr->controller->render();
try
$attr->render();
I haven't, but you might get lucky.
chillax86 replied on at Permalink Reply
Sorry but this didn't work, any other ideas?
JohntheFish replied on at Permalink Reply
JohntheFish
Why code an attribute display block?

Simpler to get and echo the attribute value directly. Usually something like:
$page = Page::getCurrentPage();
echo $page->getAttribute('your_attribute_handle');

The above is usually all you need for numbers and text. Becomes a bit more complicated for other attributes.
mnakalay replied on at Permalink Reply
mnakalay
I don't think yu can hardcode that block.

In the view function there is this piece of code
$b = $this->getBlockObject();
      if ($b->getBlockFilename()) {      
        // custom template  
      }

which will throw an error every time sinc getBlockObject() will return null and calling getBlockFileName() on null will throw an error.

When hardcoding blocks, you don't have a block object. What you have instead is a Block Type Object.

But couldn't you simply hardcode the attribute directly?

Assuming your attribute is a text attribute just do
$c = \Page::getCurrentPage();
$content = $c->getAttribute('your_attribute_handle);
echo $content->getDisplayValue();

Of course this is a simplistic example and if you look through the Page Attribute Display block's code you will see that there is more you should do depending on the type of attribute (date, topic, boolean, select...)
My point is you probably don't need to hard code that block.

Another option is to add it as a template default block, straight from the dashboard.
mnakalay replied on at Permalink Reply
mnakalay
John, you beat me to it :)
JohntheFish replied on at Permalink Reply
JohntheFish
Yes, but your answer is a bit more thorough.
chillax86 replied on at Permalink Reply
I need to output the attribute block view as it would normally. I was hoping to conditionally show the attribute based on if it has a value or not. A bit primitive and does not work but some thing like this is what I what to achieve...

$attr = BlockType::getByHandle('page_attribute_display');
$attr->controller->attributeHandle = 'project_market_sector';
$attr->controller->displayTag = 'div';
if ($attr->controller->getContent()) {
    $attr->controller->render();
 }

I tried your code and it caused an exception "call to undefined method 'getDisplayValue()'"
hutman replied on at Permalink Best Answer Reply
hutman
I don't think that this BlockType can be hard coded, if you look in the /concrete/blocks/page_attribute_display/controller.php in the view function it calls $b = $this->getBlockObject(); which will not return anything if the block isn't added to the page, so the next line will cause a fatal error.

Is there a reason you can't just add the block to the page and create a custom template that checks for a value before it outputs anything?

If that won't work, you should be able to output the value of your attribute with this code, if it's a boolean, text, or textarea (I think number will work too), but select, topic, and other attributes like that will need some extra code to display.

$c = Page::getCurrentPage();
echo $c->getAttribute('attribute_handle');
chillax86 replied on at Permalink Reply
You are so right, why don't I use custom template for this, I should of thought of that before, thanks for you help :-)