Skip to content

Instantly share code, notes, and snippets.

@geekbrit
Last active June 2, 2018 17:48

Revisions

  1. geekbrit revised this gist Jun 2, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion build_page_twig.php
    Original file line number Diff line number Diff line change
    @@ -60,7 +60,7 @@ function expandTemplate( &$root, $templates, $block ){
    foreach( $block->fields as $blockfield ){
    if( $templatefield->name == $blockfield->name ){
    switch( $templatefield->type ){
    case 'img':
    case 'img': // img not yet tested
    case 'html':
    $node->nodeValue = $blockfield->html;
    break;
  2. geekbrit revised this gist Jun 2, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion build_page_twig.php
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,6 @@ function build_page_twig(){
    }

    foreach( $pageblocks as $block ){
    echo $block->template."\n";
    $dom = $templates[ $block->template ];
    $this->expandTemplate( $dom, $templates, $block );
    }
  3. geekbrit created this gist Jun 2, 2018.
    82 changes: 82 additions & 0 deletions build_page_twig.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    <?php

    // method from a CMS object

    function build_page_twig(){

    libxml_use_internal_errors(TRUE); // this avoids errors thrown on HTML5 tags

    //
    // this section is cms-specific, could just pass in the json file name
    //
    $jsonfile = $this->config->root.'/content/'.$this->config->page.".json";
    if( file_exists( $jsonfile ) ){
    $pageblocks = json_decode( file_get_contents( $jsonfile ) );
    } else {
    $this->push_error( "No page data for ".$this->config->page );
    return;
    }

    $templates = [];

    $doc = new DOMDocument();
    $doc->loadHTML(file_get_contents('ny5/blocks/templateblocks.twig'));

    $divs = $doc->getElementsByTagName('div');

    foreach( $divs as $div ){
    if( $div->getAttribute('class') == 'bre-template' ){
    $templatename = $div->getAttribute('data-name');
    $templates[$templatename] = new DomDocument;
    $templates[$templatename]->appendChild($templates[$templatename]->importNode( $div, true ));
    }
    }

    foreach( $pageblocks as $block ){
    echo $block->template."\n";
    $dom = $templates[ $block->template ];
    $this->expandTemplate( $dom, $templates, $block );
    }
    echo $dom->saveHTML(); // change this line to save munged html off to a static file for the page
    }

    function generate_nodes( $dom ){
    foreach( $dom->childNodes as $node ){
    yield( $node );
    if( $node->hasChildNodes() ){
    yield from $this->generate_nodes( $node );
    }
    }
    }


    function expandTemplate( &$root, $templates, $block ){
    foreach( $this->generate_nodes($root) as $node ){
    if( 'DOMElement' != get_class( $node ) ){
    continue;
    }
    $rawfield = str_replace("'",'"',$node->getAttribute('data-bre-field'));
    if( "" !== $rawfield ){
    $templatefield = json_decode( $rawfield );
    foreach( $block->fields as $blockfield ){
    if( $templatefield->name == $blockfield->name ){
    switch( $templatefield->type ){
    case 'img':
    case 'html':
    $node->nodeValue = $blockfield->html;
    break;
    case 'container':
    $contained_blocks = $blockfield->blocks;
    foreach( $contained_blocks as $cblock ){
    $contained_dom = $templates[$cblock->template];
    $this->expandTemplate($contained_dom, $templates, $cblock);

    $newnode = $root->importNode($contained_dom->getElementsByTagName('div')->item(0), true);
    $node->appendChild( $newnode );
    }
    }
    }
    }
    }
    }
    }