Skip to content Skip to sidebar Skip to footer

How To Create Dynamic Content Within Syntaxhighlighter

I want to display a property name based on user input and display this inside of SyntaxHighlighter. Another post says this is supposed to be easy. JS $('#inputText').keyup(fun

Solution 1:

There were many problems that had to be overcome to get this to function. I feel this is best explained with code:

JS

<script>        
    $(function () {        
        $('#Key').popover({
            html: true,
            trigger: 'focus',
            position: 'top',
            content: function () {
                loadCodeData(true);
                console.log('content updated');
                var popover = $('#popover-content');
                return popover.html();//inserts the data into .popover-content (a new div with matching class name for the id)
            }
        });       

        $('#Key').keyup(function () {
            loadCodeData();
        });

        functionloadCodeData(loadOriginal) {
            var userData = $('#Key').val();
            var codeTemplate = $('#codeTemplate').html();
            var tokenizedValue = codeTemplate.toString().replace('$$propertyNameToken', userData);
            $('#codeContent').html(tokenizedValue);
            $('#codeContent').attr('class', 'brush: csharp');//!IMPORTANT: re-append the class so SyntaxHighlighter will process the div againSyntaxHighlighter.highlight();

            var syntaxHighlightedResult = $('#codeContent').html();//Take the content of codeContent and insert it into the div                   var popover;
            if(loadOriginal)
                popover = $('#popover-content');//popover.content performs the update of the generated class for us so well we need to do is update the popover itselfelse {
                popover = $('.popover-content');//otherwise we have to update the dynamically generated popup ourselves.
            }

            popover.html(syntaxHighlightedResult);
        }

        SyntaxHighlighter.config.tagName = 'div';//override the default pre because pre gets converted to another tag on the client.  SyntaxHighlighter.all();
    });
</script>

Markup

<div style="display: none;">
    <label id="codeTemplate">
        //Not using Dynamic object and default Section (appSettings):var actual = new Configuration().Get("$$propertyNameToken");

        //Using a type argument:int actual = new Configuration().Get&lt;int>("asdf");

        //And then specifying a Section:var actual = new Configuration("SectionName").Get("test");

        //Using the Dynamic Object and default Section:var actual = new Configuration().NumberOfRetries();

        //Using a type argument:int actual = new Configuration().NumberOfRetries&lt;int>();

        //And then specifying a Section:var actual = new Configuration("SectionName").NumberOfRetries(); 
    </label>            

    <div id="codeContent"class="brush: csharp;">

    </div>
</div>

<div id="popover-content" style="display: none">

</div>

Post a Comment for "How To Create Dynamic Content Within Syntaxhighlighter"