How do I share a two-way variable between astro and a component? - html

How do I share a 2-way variable between index.astro and a component (svelte)? I want to achieve something like this:
index.astro
---
import SomeComponent from component.svelte
let foo;
---
<SomeComponent share:var = {{ foo }} />
component.svelte
<script>
foo = "bar";
</script
index.astro
console.log(foo) // bar

Related

Displaying object into HTML in Angular

sorry for the noob question.
I created an http request and retrieved some pokemon data and put it in an object called pokemon, like so:
export class AppComponent implements OnInit{
title = 'Pokedex';
apiURL = 'https://pokeapi.co/api/v2/pokemon/1';
pokemon = {};
constructor(private http: HttpClient){}
ngOnInit(): void{
this.http.get(this.apiURL).subscribe(data =>{
const pokemon = {
name: data['name'],
id: data['id'],
abilities: data['abilities'].map( ability => ability['ability']['name']),
types: data['types'].map( type => type['type']['name']),
image: data['sprites']['front_default']
}
console.log(pokemon);
When I console log the object, it outputs in the console fine.
However, when I try to display it in an html {{ pokemon }} it just returns [object, Object]
How can I get around this?
I have tried the methods suggested below.
{{pokemon.name}}, {{pokemon[name]}} and {{pokemon?.name} display a blank page.
{{ pokemon | json }} returns an empty object, in the form of {}.
Am I perhaps doing something else wrong?
You need to use the json pipe
<pre>{{ pokemon | json }}</pre>
OR
<div> Id: {{ pokemon.id }} </div>
<div> Name: {{ pokemon.name }} </div>
<div> Abilities:
<ul>
<li *ngFor="let ability of pokemon.abilities"> {{ ability }}</li>
</ul>
</div>
<div> Types:
<ul>
<li *ngFor="let type of pokemon.types"> {{ types }}</li>
</ul>
</div>
call property as {{pokemon?.name}}
You can't use object directly. You have to access object properties by either . (Dot) notation or object[property] way.
In your case, if you want to use the property of name then use
{{ pokeman.name }}
or
{{ pokeman[name] }}

Can't Access JSON Properties Of Firestore Data With String Interpolation

I can't access the properties of my Firestore document during string Interpolation. The regular JSON output works fine though. Here's the code:
Working
<h3>{{ teacher | async | json }}</h3>
Output:
Not working
<h3>{{ teacher.name | async | json }}</h3>
and
<h3>{{ teacher.name | async }}</h3>
Output:
Typescript code
interface Teacher {
name: string;
}
teacherDoc: AngularFirestoreDocument<Teacher>;
teacher: Observable<Teacher>;
var placeToSearch = 'Teachers/'+this.selectedTeacher;
this.teacherDoc = this.afs.doc(placeToSearch);
this.teacher = this.teacherDoc.valueChanges();
You're just slightly off with the async pipe. The object needs to be unwrapped first, then you call it's properties. It should look like:
{{ (teacher | async)?.name }}
Or better yet, set a template variable to avoid using the async pipe over and over:
<div *ngIf="teacher | async as t">
{{ t.name }}
{{ t.field }}
{{ t.whatever }}
</div>
Your interface should have both name and field
interface Teacher {
name: string;
field: string;
}
<h3>{{ teacher?.name | async }}</h3>

jinja2 get whole context in single object within template

For example, I have template index.html and custom_jinja2_filter
<h1> My name is {{ name }} </h1>
<h2> I'm {{ year }} years old </h2>
<p> I'd like to pass template context to custom
filter like single object. Is it possible?
{{ ??? | custom_jinja2_filter }}
</p>
def custom_jinja2_filter(context):
name = context['name']
year = context['year']
You can pass the current context to a function marked as callable with #contextfunction:
from jinja2 import contextfunction
#contextfunction
def custom_jinja2_filter(context):
name = context.name
year = context.year
return '(c) {} {}'.format(year, name)

Angularjs web app error

The angularjs app doesn't show me any result.
It shows me :
Name City Order Total joined
{{ cust.name }} {{ cust.city }} {{ cust.orderTotal }} {{ cust.joined }}
What is the reason of this type of error !!!
Update 1:
function CustController($scope)
{
$scope.sortBy = 'name';
$scope.reverse = false;
$scope.customers = [
{joined: '240-344-55', name:'jone', city:'usa', orderTotal:'231'},
{joined: '240-344-55', name:'jone', city:'usa', orderTotal:'231'},
{joined: '240-344-55', name:'jone', city:'usa', orderTotal:'231'}
];
$scope.doSort = function(propName) {
$scope.sortBy = propName;
$scope.reverse = !$scope.reverse;
};
}
If you are using $scope approach, then you have to remove "cust." part from your view. It would be {{ name }} {{ city }} etc.
But if your view has ng-controller="CustController as cust", that means you are using "controller as" syntax, so you would need to refactor your controller code, changing $scope. to this. everywhere at least.

Hand over parameters from a MediaWiki template to a included one

I'm using the MediaWiki extension DynamicPageList (third-party) which can be used as a template:
{{#dpl:
|category=foo
|notcategory=bar
}}
I try to use this template in one of my templates which uses more parameter e.g.:
{{myTemplate
|category=foo
|notcategory=bar
|mypara1=bla
|mypara2=lala
}}
myTemplate looks like this:
do something with mypara1
...
do something with mypara2
...
{{#dpl:
|category=foo
|notcategory=bar
}}
I know my parameters but #dpl: can use one or many parameters.
How can I separate my parameters from the #dpl: ones? And how can I just hand over the parameters which belongs to #dpl:?
Thanks,
Ray
Finally I came up with the following solution:
DPL has an additional template #dplreplace. I'm using this to parse my parameters.
Call the template:
{{myTemplate
| filter=category:foo;notcategory:bar
| mypara1=bla
| mypara2=lala
}}
In the template I replace the : by = and ; by {{!}}.
{{#dpl:
| {{#dplreplace: {{#dplreplace: {{{filter}}} | /:/ | = }} | /;/ | {{!}} }}
| ordermethod = sortkey
| suppresserrors = true
}}
NOTE: {{!}} is a template which is replaced by |.
Regards;
Ray
Maybe I'm misunderstanding your issue, but you can just pass the parameters on to DPL, just like you would to a template, or another parser function. You might want to add an empty default in most cases:
myTemplate:
do something with {{{mypara1}}}
do something with {{{mypara2}}}
{{#dpl:
|category = {{{category|}}} <!-- default to empty string -->
|notcategory = {{{notcategory|}}} <!-- default to empty string -->
}}
Call it like this:
{{myTemplate
|category=foo
|notcategory=bar
|mypara1=bla
|mypara2=lala
}}
Will work with missing parameters too:
{{myTemplate
|category=foo
|mypara1=bla
|mypara2=lala
}}