Apache Velocity - if clause - templates

I use Apache Velocity for create an e-mail template.
I have a mail message that contain a table with a list of elements, for create it I had used a #foreach.
In this table I'll add a column contained a conditional string, If the element is empty the string are string1 if is not empty are string2.
This is my code:
#foreach( $item in $list )
<td style="max-width: 140px; word-wrap: break-word;">
#if(${value} not null) 'String1' #else 'String2'#end</td>
#end
Error log:
org.apache.velocity.runtime.parser.ParseException: Encountered "null" at line 25, column 131. Was expecting one of:
"[" ...
"{" ...
"(" ...
<STRING_LITERAL> ...
"true" ...
"false" ...
<INTEGER_LITERAL> ...
<FLOATING_POINT_LITERAL> ...
<IDENTIFIER> ...
"{" ...
"[" ...
at org.apache.velocity.runtime.parser.Parser.generateParseException(Parser.java:3679)
I don't find any help in stack... anyone can help me?

I think this can run in your case:
#if( $value)
<td style="max-width: 140px; word-wrap: break-word;">String1</td>
#else
<td style="max-width: 140px; word-wrap: break-word;">String2</td>
#end
Try to read this question

Related

How to test tr(table row) has onclick and style={{ cursor: "pointer" }} in react testing library

<tbody data-testid="offers-list-tablebody">
{ offers.map((Offer) => (
<tr onClick={this.handleTermsAndConditionsBtnClick} style={{ cursor: "pointer" }} key={index}>
}
I tried in this way
const tableBody = list.find("[data-testid='offers-list-tablebody']");
const tableRow1 = tableBody.childAt(0);
const clickElement1 = (tableRow1.key());
expect(clickElement1.onclick).toBeTruthy();
expect(clickElement1).toHaveStyle("cursor: pointer");
The error is Property "onclick does not exist on type string" and it is hsowing value of index when tested.
How to test this?
it is because you are assigning clickElement1 to tableRow1.key() which returns a string.
Replace clickElement with tableRow1 like below
expect(tableRow1.onclick).toBeTruthy();

converting Wildcard to Regex

I'm new in MVC, I want to perform Wildcard (*, ?) search on database. This is what I have done by using Regex:
Controller:
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
CrossWord_dbEntities db = new CrossWord_dbEntities();
public ActionResult Index(string searching)
{
if (searching == null)
{
searching = "*";
}
string regEx = WildcardToRegex(searching);
return View(db.tbl_values.ToList().Where(x => Regex.IsMatch(x.Name, regEx, RegexOptions.Singleline)));
}
public static string WildcardToRegex(string pattern)
{
return "^" + Regex.Escape(pattern).
Replace("\\*", ".*").
Replace("\\?", ".") + "$";
}
}
}
View:
#model IEnumerable<WebApplication1.Models.tbl_values>
<br /><br />
#using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
#Html.TextBox("searching") <input type="submit" value="Search" />
}
<table class="table table-striped">
<thead>
<tr>
<th>Results</th>
</tr>
</thead>
<tbody>
#if (Model.Count() == 0)
{
<tr>
<td colspan="3" style="color:red">
No Result
</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>
#item.Name
</td>
</tr>
}
}
</tbody>
</table>
i have in my database three recordes: Hello, Hero, Shalom
when i type " H* " i get the result: Hello, Hero - this works great
but when i type " *lom " i get "No Result" instead of "Shalom"
or when i type " Hell?" i get "No Result" instead of "Hello"
what i did wrong ?
You can use the following
Expression<Func<tbl_value, bool>> query = m =>
SqlFunctions.PatIndex(searching.ToLower().Replace("*", "%"), m.Name.ToLower()) > 0;
var details = db.tbl_values.Where(query);
Hope this will help you

Outputing only those rows which matches keyword

I'm trying output only those in table rows which matches keyword, but does not work. I am trying use strpos, substr and other functions. My code looks like this. Has someone idea, how to fix it and adapt that function correctly?
<?php
$start_date = strtotime($_POST['start']);
$end_date = strtotime($_POST['end']);
settype($start_date, "integ`enter code here`er");
settype($end_date, "integer");
$total = 0;
if (isset($_POST["start"]) && !empty($_POST["end"])) {
$files = glob('arch/*.log.*');
>scan files
foreach($files as $failas)
{
$file_mod_time = filemtime($failas);
if (($start_date<$file_mod_time) && ($end_date>$file_mod_time)) //looking for specific date
{
$file = #fopen($failas, "r") or die("Cannot open file!\n");
while ($filePart = fread($file, 4146)) {
preg_match_all('/(?>--\w{8}-A--).*?(?=(--\w{8}-A--)|\Z)/s', $filePart, $blockMatch, PREG_SET_ORDER);
foreach ($blockMatch as $singleBlockMatch) {
if (isset($singleBlockMatch[0])) {
preg_match_all('/.*?--\w{8}-A--\n(.*?)\s\w*?\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*?POST (\S*?) HTTP.*?--\w{8}-C--\s(.*?\s)/s', $singleBlockMatch[0], $match, PREG_SET_ORDER); // reads a blocks
/* var_dump($match); */
/* if (strpos($singleBlockMatch[0],$_POST['search'])===($match)) {*/
foreach ($match as $pos) {
$search = $_POST['search'];
$pos = strpos($singleBlockMatch[0], $search); //tries take specific rows, matching keyword
$wert = substr($singleBlockMatch[0],$pos);
} ?>
<tr>
<td><?php echo $total++; ?></td>
<td> <?php echo $wert[1]; ?></td>
<td> <?php echo $wert[2]; ?></td>
<td> <?php echo $wert[3]; ?></td>
<td> <?php echo $wert[4]; ?></td>
<?php
}
}
}
}
}
}
else {
echo "Fill a fields";
}
?>
enter image description here

How to select only inline style in html file using regex?

I tried to select the inline styles in p tag and div tag only. But no need to select td, inline styles
regex style=[\"\w\d\.\:\-\'\s\#\;]+
Input:
<p class="Test"><span style="font-family:Verdana">?</span><span style="font:7.0pt 'Times New Roman'">  </span><span>AAA</span></p>
<table cellspacing="0" cellpadding="0" style="border-collapse:collapse; margin-left:0pt">
<tr>
<td style="border-bottom-color:#808080; border-bottom-style:solid; border-bottom-width:0.5pt; border-top-color:#808080; border-top-style:solid; border-top-width:0.5pt; padding-bottom:2.85pt; padding-top:2.85pt; vertical-align:top; width:81pt">
<p class="Tabelle" style="margin-top:3pt; margin-bottom:3pt"><span style="font-family:Tahoma; font-size:9pt">Detail</span></p>
</td>
output:
style="margin-top:3pt; margin-bottom:3pt in p tag
Note:
I need to select only p tag, div tag tags inline styles.
You can try this:
Find by:
(<(?:p|div)[^<]*)(style="[^"]*")([^>]*>)
replace by:
$1$3
C# Code Sample:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
string pattern = #"(<(?:p|div)[^<]*)(style=""[^""]*"")([^>]*>)";
string substitution = #"$1$3";
string input = #"<p class=""Test""><span style=""font-family:Verdana"">?</span><span style=""font:7.0pt 'Times New Roman'"">  </span><span>AAA</span></p>
<table cellspacing=""0"" cellpadding=""0"" style=""border-collapse:collapse; margin-left:0pt"">
<tr>
<td style=""border-bottom-color:#808080; border-bottom-style:solid; border-bottom-width:0.5pt; border-top-color:#808080; border-top-style:solid; border-top-width:0.5pt; padding-bottom:2.85pt; padding-top:2.85pt; vertical-align:top; width:81pt"">
<p class=""Tabelle"" style=""margin-top:3pt; margin-bottom:3pt""><span style=""font-family:Tahoma; font-size:9pt"">Detail</span></p>
</td>
";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
System.Console.WriteLine(result);
}
}
Explanation
You get yoru data in group 1

Freemarker List inside a a table

I'm trying to generate a table with freemarker.
<table class="timeline">
<tr>
<#list children as child >
<td class="timeline-item">${child.title}</td>
<td><img class="sitelink_arrow" src="images/arrow.png"/></td>
</#list>
</tr>
</table>
This will generate an error :
Was expecting one of: ... ... ... ... ... ... ... ... <_INCLUDE> ...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... "${" ...
"#{" ...
If I put this list outside the table then it works just fine.
Any ideas ?
Your code should be look like this:
It is better if you post your code as well.
And full error also.
[#ftl]
<table id="timelineTable" class="timeline" align="left" width="100%">
[#if children?? && children?size > 0]
[#list children as child]
<tr>
<td class="timeline-item">
${child.title}
</td>
<td>
<img class="sitelink_arrow" src="images/arrow.png"/>
</td>
</tr>
[/#list]
[#else]
No Details Available
[/#if]