Welcome Guest, Not a member yet? Register   Sign In
Grabbing and echoing only fragments of views
#14

[eluser]stoefln[/eluser]
i actually doubt that its as easy as in your code to retrieve a subpart by a elements id.
finally i got it running with following code:

Code:
/**
     * Load Fragment
     *
     * This function is used to load a fragment of a "view" file.  It has four parameters:
     *
     * 1. The name of the "view" file to be included.
     * 2. An associative array of data to be extracted for use in the view.
     *
     * 3. the id of the HTML element
     *
     * 4. TRUE/FALSE - whether to return the data or load it. In some cases it's advantageous
     *    to be able to return data so that a developer can process it in some way.
     *
     * @access    public
     * @param    string
     * @param    array
     * @param    string
     * @param    bool
     * @return    mixed
     */
    public function fragmentById($view, $vars, $elementId, $return = FALSE){
        
        $html = $this->_ci_load(
                array(
                    '_ci_view' => $view,
                    '_ci_vars' => $this->_ci_object_to_array($vars),
                    '_ci_return' => TRUE
                    )
                );
        $doc = new DOMDocument();
        $docHtml = '<html>'.$html.'</html>';
        $doc->loadXML($docHtml);
        // we need a schema here since php needs a validation step befor the method "getElementById"
        $rng = '<grammar >
            <start>
                <element>
                    <anyName/>
                    <ref name="anythingID"/>
                </element>
            </start>
            <define name="anythingID">
                <zeroOrMore>
                    <choice>
                        <element>
                            <anyName/>
                            <ref name="anythingID"/>
                        </element>
                        <attribute name="id">
                            <data type="ID"/>
                        </attribute>
                        <zeroOrMore>
                            <attribute><anyName/></attribute>
                        </zeroOrMore>
                        <text/>
                    </choice>
                </zeroOrMore>
            </define>
        </grammar>';
        $doc->relaxNGValidateSource($rng);
        $element = $doc->getElementById($elementId);
        if(!$element){
            throw new Exception("No element with id \"".$elementId."\" found!");
        }
        $subpart = '';
        // loop through all childNodes, getting html
        $children = $element->childNodes;
        foreach ($children as $child) {
            $tmp_doc = new DOMDocument();
            $tmp_doc->appendChild($tmp_doc->importNode($child,true));
            $subpart .= $tmp_doc->saveHTML();
        }
        if($return == FALSE){
            echo $subpart;
        }
        unset($tmp_doc);
        unset($doc);
        return $subpart;
    }


Messages In This Thread
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 01:34 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 05:52 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 06:02 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 07:16 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 07:25 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 07:38 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 07:53 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 08:09 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 08:28 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 08:54 AM
Grabbing and echoing only fragments of views - by El Forum - 06-27-2008, 01:23 PM
Grabbing and echoing only fragments of views - by El Forum - 12-19-2008, 05:02 AM
Grabbing and echoing only fragments of views - by El Forum - 12-19-2008, 07:09 AM
Grabbing and echoing only fragments of views - by El Forum - 12-19-2008, 11:50 AM



Theme © iAndrew 2016 - Forum software by © MyBB