25 Replies Latest reply on Apr 17, 2006 7:38 AM by toddzimm

    process CF tags within retreived text field?

    toddzimm
      I've searched the forums without luck, so I hope someone can help me out...
      We have a CMS of sorts, which contains content in SQL database, editable with text fields.
      I need to run an image-related custom tag from *within* the contents of a text field. CF, of course, wants to spit out the text as text and move on. Is there a way I can get it to run the CF code contained within?
      Thanks,
      ToddZ
        • 1. Re: process CF tags within retreived text field?
          philh Level 1
          CF processes ColdFusion code that's in .cfm templates, not code that's in variables. You'd have to spit the DB contents into a .cfm file, and then call that file with CFINCLUDE.

          <cfset mycode="<cfoutput>The time is now #now()#.</cfoutput>">
          <cffile action="WRITE" file="d:\inetpub\hegepiimak\sandbox\testcfm.cfm" output="#mycode#" nameconflict="OVERWRITE">
          <cfinclude template="testcfm.cfm">
          • 2. Re: process CF tags within retreived text field?
            BKBK Adobe Community Professional & MVP
            A combination of Evaluate() and DE() might do it, thus:


            • 3. Re: process CF tags within retreived text field?
              toddzimm Level 1
              Thanks much for the input! Philh, I'm resisting the idea of generating a file to reprocess, since I might then just as well pull this content out of the CMS and store it in .cfm file to begin with...

              BKBK, I was excited to try your suggestion - I'd never used cfsavecontent before. But though it does produce the text content just fine, it still doesn't process the CF code within.

              So, I'm still trying to wrestle this alligator...
              • 4. process CF tags within retreived text field?
                BKBK Adobe Community Professional & MVP
                BKBK, I was excited to try your suggestion - I'd never used cfsavecontent before. But though it does produce the text content just fine, it still doesn't process the CF code within
                Yes, the method assumes you will do all assignments like <cfset x="myvar"> outside the cfsavecontent tag, and then write #x# within the tag, wherever the value myvar is required. If the text you wish to evaluate contains CFML tags, then evaluate it using <cfoutput>.

                • 5. Re: process CF tags within retreived text field?
                  toddzimm Level 1
                  BKBK - well, unless I'm missing something, you've just shown me the basic function of cfoutput. ;-)

                  But what I was trying to express in my question was that I'm retreiving a text field from a database, and that text field indeed contains a CF tag. (This custom tag is related to the placement of an image, this is why it's embedded in the text instead of simply being included separately.)

                  I thought maybe reprocessing the whole field with cfcontent and Evaluate, per your post, would do the trick -- (not sure why it doesn't, as it sounds fairly plausible) -- but no amount of plain ol' cfoutput seems to help my cause.
                  • 6. process CF tags within retreived text field?
                    BKBK Adobe Community Professional & MVP
                    BKBK - well, unless I'm missing something, you've just shown me the basic function of cfoutput. ;-)

                    Mi scusi. That wasn't my intention. I have tried to imagine what kind of text you might have, hence the poking in the dark. Could you let us see the code?

                    • 7. Re: process CF tags within retreived text field?
                      electroDJuk
                      i recently ran into similar "problems" when trying to build a mailing list application using ColdFusion Templates. i was trying to store the following in a database field, but when i called the content of the field back into a .cfm it wouldn't render the actual CF code.

                      any help on this will also be appreciated
                      • 8. process CF tags within retreived text field?
                        toddzimm Level 1
                        OK --(thanks again for your time!) -- here's an example, and I'll explain first. Each page requires an image to be placed somewhere within it. Our site frames all images using some CSS & Javascript (the "Rico corners" technique) and due to IE's CSS bugs, I must first sniff the width of the image and set a div to contain it widthwise. I do this all with a custom tag "WrapImage". (It all works great when CF processes the code, on static pages.)

                        So, the following represents the contents of a text field retrieved from the database:
                        • 9. Re: process CF tags within retreived text field?
                          BKBK Adobe Community Professional & MVP
                          cfsavecontent and cfoutput should do it.


                          • 10. Re: process CF tags within retreived text field?
                            toddzimm Level 1
                            Yeah, it sounds logical enough, but in practice it just isn't working. No CF tags within that text field get processed -- not my custom tag, & not the cfset tags from your example. They just appear plain as day in "view source," as unprocessed text.

                            Augh, and it seems soooooo close... there's gotta be a way...

                            I've also tried using cfsavecontent and "cfinclude variable=#content#" -- and apparently there's a bug that prevents that from working within nested templates -- it only works at the top level. (Someone else discovered this problem and noted it in LiveDocs.)
                            • 11. Re: process CF tags within retreived text field?
                              BKBK Adobe Community Professional & MVP
                              There might be some other problem at work on your side. The following code runs without any problems.



                              • 12. Re: process CF tags within retreived text field?
                                Level 7
                                > Thanks much for the input! Philh, I'm resisting the idea of generating a file
                                > to reprocess, since I might then just as well pull this content out of the CMS
                                > and store it in .cfm file to begin with...

                                But that's the only way of doing it. CF will only process CF tags that are
                                *in a file*, with the file being invoked in one of a number of ways:
                                1) requested via the URL (directly, or as a web service).
                                2) <cfinclude>
                                3) <cfmodule>
                                4) As a custom tag, eg: <cf_myfile>
                                5) <cfinvoke>
                                6) <cfobject>
                                7) createObject()
                                8) <cfhttp> (which is just performing a (1).

                                But whichever way you paint it, the stuff needs to be in a file.

                                You cannot take a string which contains CFML and execute it, other than to
                                save it as a file first, and then do one of 1-8.

                                --
                                Adam
                                • 13. Re: process CF tags within retreived text field?
                                  BKBK Adobe Community Professional & MVP
                                  that's the only way of doing it. CF will only process CF tags that are *in a file*
                                  I cannot make out whether or not this includes the last example I gave. It doesn't write to a file as Philh recommends, yet it works.

                                  • 14. Re: process CF tags within retreived text field?
                                    Level 7
                                    Yeah, but you're not answering the question:

                                    <blockquote>
                                    I need to run an image-related custom tag from *within* the contents of a
                                    text
                                    field. CF, of course, wants to spit out the text as text and move on. Is
                                    there
                                    a way I can get it to run the CF code contained within?
                                    </blockquote>

                                    So for your example to be answering toddzimm's question, you'd have to
                                    modify it so the text within the <cfsavecontent> node isn't *in your CFM
                                    file*, it's been read from a database record.

                                    <cfquery name="q">
                                    select theCfCode
                                    from aTable
                                    </cfquery>

                                    <cfsavecontent variable="content">
                                    #q.theCfCode#
                                    </cfsavecontent>
                                    <cfoutput>#content#</cfoutput>

                                    Where q.theCfCode, in this case, contains:

                                    Lorem [etc] <cf_WrapImage filename="myphoto.jpg" altText="me manufacturing
                                    a cell"> Curabitur [...] egestas.

                                    Like I said. CFML will only process if it's being read from a file. You
                                    cannot process it directly from a string.

                                    The best thing you can do is evaluate expressions (using evaluate()), like
                                    this:

                                    evaluate(DE("Lorem [etc] #cf_WrapImageFunction(filename='myphoto.jpg'
                                    altText='me manufacturing a cell')# Curabitur [...] egestas.
                                    "))

                                    So if toddzimm could use a function instead of a custom tag, then - yes -
                                    evaluate() can help you. But it CANNOT process CFML (the function could
                                    contain CFML, but not the string you're evaluating).

                                    --
                                    Adam
                                    • 15. Re: process CF tags within retreived text field?
                                      toddzimm Level 1
                                      Wow, then I've indeed been barking up the wrong tree all along, hey?

                                      It doesn't look like creating a function and using evaluate() is a realistic solution, as it wouldn't be very "editable" on large pages of information.

                                      Maybe I'll just have to move this text element from a database field to files in the file system, across the board. I guess it only creates the extra step of opening those files in a text editor rather than the CMS screen.

                                      (It seems that Philh's idea of writing a new file from the db with each page call would get a little heavy on resources, right?)
                                      • 16. Re: process CF tags within retreived text field?
                                        Level 7
                                        > I guess it only creates the extra step of
                                        > opening those files in a text editor rather than the CMS screen.

                                        It should not require an additional step for the user. When the form
                                        submits, save the rest of the data to the DB, and safe that content to file
                                        (perhaps saving a reference to the file in the DB, depending on whether
                                        that's needed).


                                        > (It seems that Philh's idea of writing a new file from the db with each page
                                        > call would get a little heavy on resources, right?)

                                        If I was going down this route, I'd write the files in two situations:
                                        1) If they were missing;
                                        2) At the point in time the data changes.

                                        There's no point in re-writing the files every time they're requested:
                                        99.999% of the time, the results will be the same, so simply do it that
                                        other 0.001% of the time (when the content changes).

                                        --
                                        Adam
                                        • 17. process CF tags within retreived text field?
                                          toddzimm Level 1
                                          Indeed... indeed...! Okay, I can do this. It should work pretty slick that way.

                                          I've learned quite a bit in the course of working this out. Thanks, Philh, BKBK, & Adam for taking the time to pitch in for a stranger. (electroDJuk, I hope you make it back to read the results!)
                                          • 18. process CF tags within retreived text field?
                                            BKBK Adobe Community Professional & MVP
                                            I indeed wondered why Toddzimm was having such difficulties. I now understand the question to be as follows. Given the string,

                                            <cfset contentFromDB = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus ultricies diam ut magna. Praesent pede risus, congue non, pellentesque non, nonummy sed, sapien.<br><br><cf_WrapImage filename=""cell_staff.jpg"" frameType=""B"" altText=""a manufacturing cell team""><br><br>Curabitur neque mi, vulputate eget, suscipit nec, blandit quis, dolor. Cras elementum.Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.">

                                            does there exist Coldfusion code one can put on the current page to run the custom tag?

                                            I can now see the source of the misunderstanding. I have added extra double quotes to escape the double-quotes around the attribute values. Toddzimm's version of the text, where the quotes are not escaped, gives the impression that we're already in a page which displays the text from the database. Hence, my code suggestions.

                                            I think the reason for the failed attempts is that Coldfusion tags and functions for outputting data have been tag-defanged. By that I mean, they have been designed so as to ignore any tags that occur in the output. For example, in the following block of code, Coldfusion ignores any tags that occur in the output. Unknown tags don't even cause an exception.

                                            <someTag> <cfoutput><someTag></cfoutput><br>
                                            <someTag /> <cfoutput><someTag /></cfoutput><br>
                                            <someTag></someTag> <cfoutput><cfscript>writeoutput("<someTag></someTag>");</cfscript></cfoutput><br>
                                            <cfscript>writeoutput("<cfset x=2><cfscript>writeoutput(x);</cfscript>");</cfscript> <br>
                                            <cfset y="<cf_myCustomtag attr1=1 attr2=2>">
                                            <cfoutput>#y#</cfoutput>
                                            <cfset z="<cfset x=2><cfscript>writeoutput(x);</cfscript>">
                                            <cfoutput>#Evaluate(DE(z))#</cfoutput>

                                            Coldfusion will therefore ignore the custom tag <cf_wrapimage>, when it occurs in the output. But then, that is where it occurs when we apply <cfoutput>, writeoutput(), evaluate(), etc.

                                            • 19. Re: process CF tags within retreived text field?
                                              BKBK Adobe Community Professional & MVP
                                              I would recommend the following procedure, which doesn't require you to create files. It therefore avoids any extra performance costs. It is a variation on ElectroDJuk's theme. All you need to do to set up is to:

                                              - Create the folder folderContainingCustomTags in the directory containing currentFile.cfm;

                                              - Copy the custom tags into it.
                                              • 20. Re: process CF tags within retreived text field?
                                                Level 7
                                                Interesting.

                                                Yes, if you can guarantee that the CFML you need to execute is just custom
                                                tage calls, this'll do the trick. Nice one.

                                                Given the rules you're setting up for this (custom tags need to be in a
                                                specific subdir), can you not dispense with the <cfimport>, and just use
                                                <cfmodule>? That way you would not have to have a case statement for each
                                                possibility of tag, just check if the file exists in your pre-defined
                                                location, and if so, call it? <cfmodule> has the beenfit of being able to
                                                take run-time values for its template, whereas custom tag or imported tags
                                                cannot. This is slightly more scalable.

                                                Good lateral thinking, BKBK!

                                                --
                                                Adam
                                                • 21. Re: process CF tags within retreived text field?
                                                  BKBK Adobe Community Professional & MVP
                                                  Given the rules you're setting up for this (custom tags need to be in a
                                                  specific subdir), can you not dispense with the <cfimport>, and just use
                                                  <cfmodule>? That way you would not have to have a case statement for each
                                                  possibility of tag, just check if the file exists in your pre-defined
                                                  location, and if so, call it? <cfmodule> has the beenfit of being able to
                                                  take run-time values for its template, whereas custom tag or imported tags
                                                  cannot. This is slightly more scalable.


                                                  I have just given it a whirl. I think this is the best solution so far.

                                                  • 22. process CF tags within retreived text field?
                                                    toddzimm Level 1
                                                    OK, well, I've got the cfimport variety of this working, so to speak -- and it's amazing to see it do the unpossible -- but it still doesn't quite fit my situation. It extracts and displays the image specified in the article text -- separately, outside the flow of text. And it depends on having only one image per article, which must always be in a specific position (4th, as we have it here) in the parsed list of tags.

                                                    If we were creating a catalog, or if we wanted to simply lead or follow every site article with a single image, this might fit the bill great. But we have articles with X number of images placed within the flow of text. That's why I hoped I could give editors a single, simple example for inserting images with parameters for frame type, alt text, maybe alignment & link URL... whatever: <cf_FrameImage filename="driven.jpg" frameType="A" altText="foo">

                                                    If I understand correctly, learning how to use cfmodule would let me pass those parameters -- eliminating the need to create a unique custom tag for... every... image.. on the site.

                                                    But, I don't see that it would help me present an article with images placed within the text flow.

                                                    Also, attached is what my current custom tag FrameImage.cfm looks like, in case you're puzzled as to why I'd go to these lengths. It's all about IE's bug in handling unspecified DIV widths...

                                                    • 23. Re: process CF tags within retreived text field?
                                                      BKBK Adobe Community Professional & MVP
                                                      But, I don't see that it would help me present an article with images placed within the text flow.
                                                      Again using the example I gave, the following code will output the entire contentFromDB , including text, line breaks and custom tag.

                                                      The code was based on the contentFromDB variable that I created. You should, of course, adapt it to your situation. That is, taking account of the common structure(delimiters, tags, line breaks) of the text blocks that you pull from the batabase.





                                                      • 24. Re: process CF tags within retreived text field?
                                                        BKBK Adobe Community Professional & MVP
                                                        It's all about IE's bug in handling unspecified DIV widths...
                                                        Aren't there box hacks to deal with that in CSS?

                                                        • 25. Re: process CF tags within retreived text field?
                                                          toddzimm Level 1
                                                          Well, there are hacks to deal with a box model problem in IE-Win versions previous to 6, where IE thinks the "width" of an element actually includes the width of the padding and border, too.

                                                          The different problem I'm facing is that when the width of a nested div is unspecified, IE (even current versions) defaults to maximum, rather than minimum. So it fills 100% of the width of the parent div. This is a problem when you want an inline div to contain an image and have text wrap around it. The solution is simply to specify a div width. But since I'm working on a CMS holding articles that can contain images of X width, I have to dynamically obtain that width and specify it for the div (CFX_ImageInfoMX does that for me.)

                                                          And because I'm working in an automated CMS system, where there is just one template to display all articles, code written for an exact [contentFromDB that you created] doesn't do the trick. There is no common content structure. It has to work for #contentFromDB#, whatever that field happens to contain. It could be X paragraphs with Y line breaks and Z images.

                                                          So, I think I'm still at the solution of generating .cfm files to contain the article content...