In ColdFusion I am creating and saving a file, then later looping over characters in the file to display part of it. This is almost working, but the loop is sometimes inserting characters that are formatting rather than just the output. And sometimes it is losing the formatting. Here are the original and the version as read:
The code:
<cfset colvalue = getPageContext().getRequest().getParameterValues('#col#')>
<cfset repa = colvalue[1]>
<cfloop file="#reppath#moxrep/#repa#.cfm" index="chunk" characters="500">
<cfoutput>#chunk#</cfoutput><br>
</cfloop>
Am I doing something wrong in the code? Is there a bug in the ColdFusion loop over file? And if so, is there a workaround?
<cfloop .. characters="500">
It is because your loop uses the "characters" attribute, which limits the number of characters "..read during each iteration of the loop..". That would be fine for a text file. However, since the file content is HTML, it breaks when you try and insert the <br> at an arbitrary position. That causes part of the HTML code to be displayed instead of rendered. For example:
<div <br> style="text-align: left; ">This will not render correctly</div>
That said, it begs the question why read the content line by line instead of just displaying the whole file?
Update:
You really cannot parse HTML with basic string functions or regular expressions - not with any reliability. Encountering a new line character does not necessarily mean you have reached the end of a particular block of HTML code. It is perfectly valid for an HTML element to span multiple lines. Plus, HTML elements are frequently nested. So it is near impossible to identify the "logical" endpoints using string functions (which is basically what the cfloop is doing) alone.
Instead, I would recommend using a tool like JSOUP which is specifically designed for parsing HTML. Once you have parsed the document, it is very easy to access specific elements or sections of the HTML.
Related
Here is some code:
<!---this is identical in several programs --->
<cfset i = 0>
<cfoutput>
<cfloop array = "#colvalue#" index = "val">
<cfset i = i + 1>
<cfset fieldname = colarr[i]>
<cfset modnum = i%basenum>
<script>
putval2('#val#', '#i#')
</script>
<!--- stuff which is different in each program --->
</cfloop>
</cfoutput>
This is working fine.
I put the identical code into a separate module called putback.cfm and rewrote this as:
<cfinclude template = "putback.cfm">
<!---stuff which is different in each program --->
</cfloop>
I got the error message "No matching start tag for end tag [cfloop]".
I thought the include meant that "putback.cfm" would be picked up and dumped into that spot in the program. But since it is not recognizing that the end tag cfloop goes with with beginning cfloop in putback.cfm, apparently it is not as transparent a process as I have understood.
Can anyone explain why this is not working and/or offer a workaround.
I thought the include meant that "putback.cfm" would be picked up and dumped into that spot in the program.
Close, but includes are placed and processed at runtime only. You are not reaching that point because of an expression error (by the CFML parser).
it is not recognizing that the end tag cfloop goes with with beginning cfloop in putback.cfm
The parser runs through your .cfm templates to build the AST before executing any code (runtime). The loop is never closed according to the parser because the include, which is another .cfm template, is inspected separately and never touched in its included state(s).
The Fix
<!--- stuff which is different in each program ---> is the part that should be included instead. However, this kind of coding practice is considered bad and eventually leads to Spaghetti code as you require, reuse and depend on variables inside of the loop. Your include should not be aware of its surroundings. If you absolutely have to do it this way, at least use cfmodule to pass variables to your include.
The proper way of solving this problem is by using an interface. You specify what you need, provide what you have and let <!--- stuff which is different in each program ---> serve this interface. This code structure would be based on cfcomponent and its cffunctions, implementing cfinterface.
I am using react table. I am calling a method on API (.NET), which concatenates some fields and now I need to display this concatenated field in react table column.
From API I tried to put /n in the column so that it renders a new line on UI (inside react table). But no luck. Example: I need to display
This field is calculated by adding two numbers 10 and 20. Since the
result is greater than 15 you cannot perform this operation.
In the displayed text, what I want is "Since" should start in a new line in react table.
I tried to put {'\n'} before "Since" but it did not work. Any ideas how to achieve this?
The '\n' escape character does not work in HTML. You have to work with
other HTML solutions like using <br> or divs to get a multiline
implementation working.
You solve this quite easily. when getting the string append a special char like the comma (,) or some other char that you are sure would not be used in your original text.
Then get that string to a js variable in your react app.
let multiLineString = "This field is calculated by adding two numbers 10 and 20. , Since the result is greater than 15 you cannot perform this operation";
let lines = multiLineString.split(',')
let linesHTML = lines.map((line)=><div>{line}</div>);
now render this variable anywhere you want, you will get a multi line string implementation.
Of course the better approach would be to get a json array of values from the back end.
I have a string that will be mixed with text and html markup that I would like to parse and deal with accordingly. The HTML markup with include references to record ID's that I can use later when compiling text and mention segments for a post.
For the most part, I'm understanding how to split out the individual segments, but I don't know how to get them back in the correct order in which they came.
An example string:
Hi <span contenteditable="false" data-mention="#005i0000003KteOAAS">First Name</span>
I can parse into 'Hi ' and '005i0000003KteOAAS' individually, but how do I get them back in the original order?
I'm using a regex like this currently:
<(?i).*?<\\/.*?>
I'm using an analog of Shopify and I'm stuck with syntax of Liquid.
I need to output in the template the field with an id product[product_field_values_attributes][][value]
So I need to write a loop to get the i value of this array.
I'm confused with this empty element in the brackets.
I've looked through the examples of loop syntax in Liquid but all of those arrays are simple and they are not my case.
For example, the Title field has id product[title] and in Liquid template i call this variable product.title and it works fine.
But all my tries to write a loop for this array failed.
Please, help to write a loop to get the values of the array stated above.
Try outputting the array directly onto the page using {{ product }} or {{ product[product_field_values_attributes] }} somewhere in the HTML. That will do a JSON-like string representation of the array. From there you can figure out what the keys for the array are.
I'm not sure what you're saying about the i value. You don't reference i anywhere else in your question. If you can clarify that then we can see if something can be done about it.
In my application I have Terms for Apps. Before I used to put this in front-end with HTML .
But now I need to put this text in database in a column called AppTerms inside table called App.
I referred to this link in stackoverflow and followed like this :
UPDATE [AppsDatabase].[dbo].[App]
SET AppTerms = 'This App has no minimum term.' + CHAR(13) +
'This App is built and is supported through the standard way of using the company online support.' + CHAR(13) +
'Incorrectly formatted data will not be formatted but may be charged for.'
WHERE AppID = 8
GO
But I am not getting line breaks here. And also can someone tell me how can i put bullet points in each line?
If you are showing HTML you need to store </br> instead of CHAR(13).
Displaying this text in HTML will yield the correct display. In addition you can wrap a paragraph in <p>...</p>.
For bullet lists look at http://www2.gol.com/users/billp/articlehtml/bullet.html.
This is under the assumption that you take the text out and display in HTML.
It might be too late, yet, I had the same problem...
The solution I came up with is to store paragraphs(or lists... or whatever) in a separate child table and handle the formatting at the server programming language level.
It should work. In HTML you could use the </br> tag.
You want the application to render HTML with line breaks, but you're storing plain text.
You need to store HTML instead. Encode the line breaks using the <br> element.
Try this:
UPDATE [AppsDatabase].[dbo].[App]
SET AppTerms = 'This App has no minimum term.<br>' + CHAR(13) +
'This App is built and is supported through the standard way of using the company online support.<br>' + CHAR(13) +
'Incorrectly formatted data will not be formatted but may be charged for.'
WHERE AppID = 8;
If you want to encode bullet points, check out the <ul> and <li> elements.
This is a data formatting issue more than a SQL Server issue. You would have had the same problem if you tried to store the data in a file instead of a table.
The W3C specification for HTML Text explains how white space is handled in HTML:
In HTML, only the following characters are defined as white space
characters:
ASCII space ( )
ASCII tab ( )
ASCII form feed ()
Zero-width space ()
Line breaks are also white space characters.
[...] user agents should collapse input white space sequences when producing output inter-word space.
Line breaks are equivalent to spaces, and multiple white space characters are collapsed to a single space in the output.