I am wondering if there is a way to render a template and pass query data to it? Say I am doing an ajax style search that posts to a cfc. What I would love to do is run the query and just do this:
return "<cfinclude template='searchlist.cfm'>"
This does work, but none of the data gets populated in the searchlist.cfm Is there a way in lucee to pass query data with that? Searchlist.cfm is just a table row with cells that contain the data.
Including like that shouldn’t work. Including a template shouldn’t return a value per se, because its not an object. What are you trying to do? Or what is the value (or the content) you are expecting to get from that included template?
I have standard table the loops query results to output rows. I have a search input that I am using to filter the results. I am using HTMX to post to a cfc and return updated query results depending on what was filtered. I am trying to find the best way to return the results. Do I need to specify all the httml for the table row output in the return from the cfc, or is there an easier way to just return the data and somehow update the results on the page using the same HTML. If you need example code let me know.
try <cfsavecontent> :: Lucee Documentation?
cfsavecontent( variable="test" ){
include template="searchlist.cfm";
}
return(test);
Thanks for the suggestion. Playing around now. I am getting an error Can’t cast struct to string. Is this how I should be returning this?
public function searchlist() {
var requests = {};
var requests.data = "
SELECT * from dbo.requests";
if (isDefined("empidsearch") && len(trim(empidsearch))) {
requests.data &= " AND dbo.request.empid LIKE :empidsearch";
requests.data = queryExecute(requests.data, { empidsearch: { value: "%#empidsearch#%", cfsqltype: "CF_SQL_VARCHAR" } });
} else {
requests.data = queryExecute(requests.data);
}
cfsavecontent( variable="requests.data" ){
include template="/views/request/searchlist.cfm";
}
return(requests.data);
}
Looks like you are returning the query data, and not the string saved by the savecontent. We can’t be sure how to make your code work, because we do not know what your included template has as source. Try this:
public string function searchlist() {
var requests = {};
var requests.data = "
SELECT * from dbo.requests";
if (isDefined("empidsearch") && len(trim(empidsearch))) {
requests.data &= " AND dbo.request.empid LIKE :empidsearch";
requests.data = queryExecute(requests.data, { empidsearch: { value: "%#empidsearch#%", cfsqltype: "CF_SQL_VARCHAR" } });
} else {
requests.data = queryExecute(requests.data);
}
cfsavecontent( variable="result" ){
include template="/views/request/searchlist.cfm";
}
return result;
}
`However, in my opinion would be better to use a function for that, instead of a include file, but your solution should also work properly.
Here is my full setup. I am still struggling with the output of requests in the partial. It works when I first load the page but when I search I get the error Can’t cast Complex Object Type Struct to String. Not sure what the best way to do this is.
Search field:
<input type="text" id="empidsearch" name="empidsearch"
hx-post="#buildURL('request.searchlist')#"
hx-trigger="keyup changed delay:500ms, search"
hx-target="##search-results"
hx-indicator=".htmx-indicator">
request controller FW/1:
public any function searchlist(rc) {
var empidsearch = rc.empidsearch;
rc.requests = variables.requestService.searchlist(empidsearch);
variables.fw.renderData( "json", rc.requests);
}
request service FW/1:
public string function searchlist() {
var requests = {};
var requests.data = "
SELECT * from dbo.requests";
if (isDefined("empidsearch") && len(trim(empidsearch))) {
requests.data &= " AND dbo.request.empid LIKE :empidsearch";
requests.data = queryExecute(requests.data, { empidsearch: { value: "%#empidsearch#%", cfsqltype: "CF_SQL_VARCHAR" } });
} else {
requests.data = queryExecute(requests.data);
}
cfsavecontent( variable="result" ){
include template="/views/request/searchlist.cfm";
}
return result;
}
Searchlist.cfm
<cfoutput>
<cfloop query="#requests#">
<tr class="border-b dark:border-gray-700">
<td class="px-4 py-3">#ACTIVEIDENTIFICATIONID#</td>
<td class="px-4 py-3">#displayName#</td>
<td class="px-4 py-3">#friendlyName#</td>
<td class="px-4 py-3">#dateformat(startdate, 'mm/dd/yyyy')#</td>
<td class="px-4 py-3">#dateformat(enddate, 'mm/dd/yyyy')#</td>
<td class="px-4 py-3">#username#</td>
<td class="px-4 py-3">#datetimeformat(createdat, 'mm/dd/yyyy h:mm tt')#</td>
</tr>
</cfloop>
</cfoutput>
Please, write here the line of code that gives you this error.
It is in searchlist.cfm on the cfloop start. Line 4.
requests.data = queryExecute()
your query is in “requests.data” variable, not in “requests”.
1 Like
that’s it! It works now. I don’t know why I was doing that. Thanks for the help.
2 Likes