# Przykład Użycia Template Helpers
## Przykład 1: Podstawowe Parsowanie
```typescript
import { TemplateParser } from './template-parser';
const parser = new TemplateParser();
const template = `
{{ title }}
`;
const elements = parser.parse(template);
console.log(elements);
// Output: Drzewo elementów z wyodrębnionymi atrybutami i text nodes
```
## Przykład 1b: Parsowanie z Text Nodes
```typescript
import { TemplateParser } from './template-parser';
const parser = new TemplateParser();
const template = `
Some text before
Inside div
Some text after
`;
const elements = parser.parse(template);
for (const node of elements) {
if ('type' in node && node.type === 'text') {
console.log('Text node:', node.content.trim());
} else {
console.log('Element:', node.tagName);
}
}
// Output:
// Text node: Some text before
// Element: div
// Text node: Some text after
```
## Przykład 2: Przetwarzanie Atrybutów
```typescript
import { TemplateParser } from './template-parser';
import { InputBindingHelper } from './input-binding-helper';
const parser = new TemplateParser();
const inputHelper = new InputBindingHelper();
const template = '
Content
';
const elements = parser.parse(template);
parser.traverseElements(elements, (element) => {
for (const attr of element.attributes) {
if (inputHelper.canHandle(attr)) {
const result = inputHelper.process({
element,
attribute: attr,
filePath: 'example.component.html',
});
console.log('Processed:', result);
}
}
});
```
## Przykład 3: Transformacja Control Flow
Wejście:
```html
@if (isLoggedIn) {
Welcome back!
} @else if (isGuest) {
Welcome guest!
} @else {
Please log in
}
```
Wyjście po transformacji:
```html
Welcome back!
Welcome guest!
Please log in
```
## Przykład 4: Kompletny Szablon
Wejście:
```html
{{ user.name }}
@if (user.isAdmin) {
Admin
}
{{ post.title }}
```
Po przetworzeniu przez TemplateProcessor:
```html
{{ user.name }}
Admin
{{ post.title }}
```
## Przykład 5: Własny Helper
```typescript
import { BaseAttributeHelper, AttributeProcessingContext, AttributeProcessingResult } from './base-attribute-helper';
import { AttributeType, ParsedAttribute } from './template-parser';
export class DataAttributeHelper extends BaseAttributeHelper {
get supportedType(): string {
return 'data-attribute';
}
canHandle(attribute: ParsedAttribute): boolean {
return attribute.name.startsWith('data-');
}
process(context: AttributeProcessingContext): AttributeProcessingResult {
// Konwersja data-* atrybutów na format Angular
const dataName = context.attribute.name.replace('data-', '');
return {
transformed: true,
newAttribute: {
name: `[attr.data-${dataName}]`,
value: context.attribute.value,
type: AttributeType.INPUT_BINDING,
},
};
}
}
// Użycie w TemplateProcessor:
// this.attributeHelpers.push(new DataAttributeHelper());
```
## Przykład 6: Debugowanie
```typescript
import { TemplateParser } from './template-parser';
const parser = new TemplateParser();
const template = '
Text
';
const elements = parser.parse(template);
parser.traverseElements(elements, (element) => {
console.log(`Element: ${element.tagName}`);
console.log(`Attributes count: ${element.attributes.length}`);
element.attributes.forEach(attr => {
console.log(` ${attr.name} [${attr.type}] = "${attr.value}"`);
});
if (element.textContent) {
console.log(` Text: ${element.textContent}`);
}
});
// Output:
// Element: div
// Attributes count: 2
// [title] [input] = "value"
// (click) [output] = "handler()"
// Text: Text
```
## Przykład 7: Wykrywanie Wszystkich Typów Atrybutów
```typescript
import { TemplateParser, AttributeType } from './template-parser';
const parser = new TemplateParser();
const template = `