How do I get the value of a query string? - freemarker

How do I access the value of a query string from a URL in a Magnolia CMS FreeMarker template? For example, given the following URL:
https://bobswebsite.com/about?campaign_id=90fsdfdsf80
How do I retrieve the value of the campaign_id query parameter?

Access the parameter from the ctx object:
[#assign campaignParameter = ctx.campaign_id!]
If you need the whole query string, use the getAggregationState method:
[#assign wholeQueryStr = ctx.getAggregationState().getQueryString()!/]

Use the ctx.getParameter templating function. For example:
[#assign queryString = ctx.getParameter("campaign_id")!]
Source: Rendering context objects

Related

Error: BabelPluginRemoveGraphQL: String interpolations

I am getting below error when trying to pass variable dynamically in gatsBy Graphql.
Error
Module build failed (from ./node_modules/gatsby/dist/utils/babel-loader.js):
Error: BabelPluginRemoveGraphQL: String interpolations are not allowed in graphql fragments. Included fragments should be referenced as `...MyModule_foo`.
Query
let mytext = 'welcome'
let myQuery = graphql`query($text: String = "${mytext}") {
allGhostPost : allGhostPost(filter:{title:{eq: $text}}) {
edges {
node {
id
slug
}
}
}
}`
Please help!!!
Inserting arbitrary text into queries like this is a well-known security issue and the Babel plugin is almost certainly right to forbid it. GraphQL defines a JSON-over-HTTP payload format that allows passing the variables separately (encoded as JSON objects to minimize the possibility of injection attacks).
You don't show what's actually making the query, but it should have a place to add a map of GraphQL variables. (For example, the graphql-js reference implementation includes a variableValues parameter to its top-level graphql function.) Remove the = "${mytext}" part of the query, and instead use a variables object like {text: mytext}.

Queries allow you to specify Params; But how do I pass params?

The documentation gives these examples of query definitions. My understanding is items starting with _$ are parameters you can pass to the query.
The only examples I can find of the Query() function inside the transaction processors do not provide parameters.
How can I pass params to my query?
Ala let result = Query('MyGreatQueryReqParam`, name = "john");
As answered on Rocket Chat ...
The buildQuery and Query() methods are covered with examples at the bottom of this doc: https://hyperledger.github.io/composer/latest/api/runtime-api
But the short answer is:
return query('Q1', { inputValue: 'blue' })

Passing several query parameters to a GET endpoint in .netcore web api

I have an application where there will be several parameters passed to my endpoint for searching, these parameters are not defined because they are dynamically generated so i cannot map it to a specific model. What would be the best way to map any query parameters into my GET endpoint?
[HttpGet]
public CustomResponse GetResults({something here that will map the parameters?})
{
//perhaps a dictionary? a collection of some sort?
}
Then I need to get all those keys and values and search the database for anything containing that and as i said it could be anything.
So I could pass something like?
/api/Merchandise/GetResults?sku=30021&cupsize=medium&color=red&location=south& {and all the dynamic fields which could be anything}
HttpRequest object has Query property that is an IQueryCollection and holds all passed query parameters.
In other words, in your action method you may do:
[HttpGet]
public CustomResponse GetResults()
{
var queryParams = HttpContext.Request.Query;
// directly get by name
var value1 = queryParams["parameter_name"];
// or queryParams.TryGetValue()
foreach (var parameter in queryParams)
{
string name = parameter.Key;
object value = parameter.Value;
}
}
You could map it to JObject, which is like a Dictionary.
Don't forget:
using Newtonsoft.Json;

Trying to Create dynamic laravel query string but getting error

$string='->get()';
$data_asset=DB::table('assets').$string;
Object of class Illuminate\Database\Query\Builder could not be converted to string
I've got something similar to what you're trying to achieve.
In your code, your $data_asset=DB::table('assets') returns a QueryBuilder, which builds a string in the background with your query parameters.
To do what you're after:
// Create a QueryBuilder instance with a table
$q = DB::table('users');
// I actually set this elsewhere, but for this example I've set it here
$params = ['email'=>'joe.bloggs#example.com','toSql'=>true];
// Use the QueryBuilder instance again, specifying a criteria
$q->where('users.email', '=', $params['email']);
if ( isset($params['toSql']) ):
// Returns my query as it's full SQL string (minus parameters)
return $q->toSql();
else:
// Returns a Collection
return $q->get();
endif;
This allows you to build a dynamic QueryBuilder builder.

Initializing a django modelform with data from url

I have two models for store and city:
class City(models.Model):
name = models.CharField()
slug = models.SlugField()
class Store(models.Model):
name = models.CharField()
slug = models.SlugField()
city = models.ForeignKey(City)
If I have my Add Store url designed as
r^'mysite.com/city/(?[-\w]+)/venue/add$'
where the represents the City.slug field can I initialize a StoreForm that automatically populates the Store.city field from the url data?
In your template, define your link as follows.
{%url somename relevant_slug_text%}
Or :
href='/mysite.com/city/{{slug_text}}/venue/add'
In your url conf, define your url like:
url(r^'mysite.com/city/(?P<slug_text>\w+)/venue/add$', 'func_name', name='somename')
So, you can pass the value of relelvant_slug_text variable to your url as slug_text, and in your function definiton:
def func_name(request, slug_text):
So , you can pass text value to your funcrtion with slug_text parameter...
EDIT:
There are tow ways...
One:
Crete your city selection form using ModelForm..., then inthe second step, use posted data to populate your form again like:
form = StoreForm(request.POST)
then you can render this form to your template...
But if it is not possible o use this, yo ucan do the following...
Since you are using ModelForm to create your forms:
class StoreForm(forms.ModelForm):
# your form field definitions are here
im your function, just override city field, but at this point, since you use modelform, your form thml will be created as
<select>
<option value="id of record">"unicode value of record"</option>
So, you have record id's as option values. And you have slug_field values to initialize your field. So you have to get related city to use it...
my_city = City.objects.get(slug=<your_slug_value>)
Now you can override the form, but you must do it before you pass your form to a variable to render to your template...
StoreForm.base_fields['city'] = forms.ModelChoiceField(queryset=City.objects.all(), initial=my_city)
form = StoreForm()

Resources