I have requirements to display Name of author who published the page and published date on page how to fetch those details can anyone suggest.
Solved! Go to Solution.
Hi @ashar02 ,
I am assuming you want to achieve this using a component. So you must have a Sling Model for your component where you can write your logic. Logic will go like this:
@ScriptVariable
private Page currentPage;
@Shivam153 Yes I am using component and how to fetch the lastReplicated(publish page) date of a page in the format 4/7/2023.
Write a sling model for your component and read the last published by property from jcr:content and last:modified. Use simple date format to format the date and pass it to front end. You can also directly print it on html using sightly as page api is available on sightly.
@ashar02 Code snippet for getting the date and changing to the format.
private static final String DATE_FORMAT = "dd MMMM yyyy"
@ScriptVariable
private Page currentPage;
@PostConstruct
void init() {
ValueMap valMap = currentPage.getProperties();
if (null == valMap) {
return;
}
String createdBy = valMap.get(JcrConstants.JCR_CREATED_BY, String. class);
GregorianCalendar publishedDate = valMap.get(NameConstants.PN_PAGE_LAST_PUBLISHED, GregorianCalendar.class);
String publishedDateString = DateUtils.dateFormat(publishedDate , Date.class), DATE_FORMAT);
}
private static String dateFormat(final GregorianCalendar gregorianCalendarDate, final String dateFormat) {
final SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(gregorianCalendarDate.getTime());
}
Hope this helps!
Thanks
@ShaileshBassi How to print name and date in html?
Hi @ashar02
I am not sure if you have followed the replies given by other SMEs. One quick question ! Hope you have a sling model for your component.
In the sling model write two getter methods for author and date.
For e.g something like below
public String getAuthor() {
return author;
}
public String getPublishedDate() {
return publishedDate;
}
In your HTL you can call these methods using
${modelname.author}
${modelname.publisheddate}
The logic to get the date and format them are already given by other SMEs. Combine all these steps and you should get your required output.
Thanks
Veena ✌
Views
Likes
Replies